Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I was looking for a way to start elevated (i.e. administrator permissions) and/or invisible processes via batch in Windows. It turns out that this is kind of impossible with batch only, so I googled some vbs stuff and hacked it all together into a neat command line power utility.

The interface is as follows:

exec [/action: elevate, open, read, print] [/display: hide, show] exe [arguments]

/action defaults to open. /display defaults to show. exe is the path to an executeable, could be a script, image, whatever as well. arguments are applied to the exe.

Example: This starts a background node.js deamon with admin rights.

exec /action:elevate /display:hide node myDeamon.js

Everything works just fine, but I'm pretty sure that it's not perfect yet, as I basically have no idea about VBScript. I'd like to hear your opinions or ideas!

Set objShell = CreateObject("Shell.Application")

intOffset = 0

' get action
strAction = "open"
If (WScript.Arguments.Named.Exists("action")) Then
  intOffset = intOffset + 1
  Select Case WScript.Arguments.Named.Item("action")
        Case "elevate" strAction = "runas"
        Case "open" strAction = "open"
        Case "read" strAction = "read"
        Case "print" strAction = "print"
        Case Else strAction = "open"
    End Select
End If

' get display mode
intMode = 1
If (WScript.Arguments.Named.Exists("display")) Then
    intOffset = intOffset + 1
    Select Case WScript.Arguments.Named.Item("display")
        Case "0" intMode = 0
        Case "false" intMode = 0
        Case "hide" intMode = 0
        Case "hidden" intMode = 0
        Case Else intMode = 1
    End Select
End If

' get application to execute
strApplication = WScript.Arguments(intOffset)

' get arguments
strArguments = ""
For i = intOffset+1 To WScript.Arguments.Count-1
    strArguments = strArguments & WScript.Arguments(i)
Next

' execute application
If (WScript.Arguments.Count >= 1) Then
    objShell.ShellExecute strApplication, strArguments, "", strAction, intMode
Else
    WScript.Quit
End If

Gist

share|improve this question
1  
silvinci, please include the relevant code directly in your question as required by the faq. –  codesparkle Dec 14 '12 at 9:18
    
Done. Just thought it would be too lengthy. –  buschtoens Dec 15 '12 at 1:46

1 Answer 1

The number one improvement you can make is to include Option Explicit as the first line of your script and explicitly declare your variables using Dim. This will save you a lot of heartache in the future (e.g., if you make a typo in a variable name, it becomes a runtime error, instead of just silently creating a new variable).

share|improve this answer
    
This sounds pretty reasonable. Thanks. Do I have to Dim in For statements? –  buschtoens Mar 17 '13 at 12:09
    
If you have a statement For i = 1 To 10, then you must declare i with Dim i, if that's what you're asking. –  Cheran Shunmugavel Mar 17 '13 at 19:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.