HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Power utility for starting processes

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
startingutilitypowerforprocesses

Problem

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 strApplicati

Solution

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).

Context

StackExchange Code Review Q#19596, answer score: 2

Revisions (0)

No revisions yet.