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