Best practice for running .exe with many parameters from vbscript

I have a shell script that I converted to vbs.   After some trial and error I got the script to work but I am hoping there is niftier way to accomplish passing parameters to an exe

The shell script looks like this…sets 12 parameters and then runs the

set PERCminiexe="C:\Perc\mini.exe"

set PERCminiopt="-f"

set PERCconfigfile="C:\Perc\config.txt"

set Monitor="MIS_Monitor"

set Entity="HR_Admin"

set Host="myserver.com"

set Service="HR"

set Alias="DEV52"

set xDate=""

set xTime=""

set State="ALERT"

set Text="This is a test firing from WMI from cmd"

%PERCminiexe% %PERCminiopt% %PERCconfigfile% %Monitor% %Entity% %Host% %Service% %Alias% %xDate% %xTime% %State% %Text%

When trying to run this in vbs I had to include a bunch of spaces and quotes to make the command run.  Result looks something like this.  I am looking for smarter way

PERCminiexe="C:\Perc\mini.exe"
PERCminiopt="-f"
PERCconfigfile="C:\Perc\config.txt"
Monitor="MIS_Monitor"
Entity="HR_Admin"
Host=" myserver "
Service="HR"
Alias="DEV52"
xDate=FormatDateTime(Date() ,2)
xTime=FormatDateTime(now() ,4)
State="ALERT"
Text="This is a test firing event from WMI using vbs"

Set WshShell = CreateObject("WScript.Shell") 
WshShell.run Chr(34) & PERCminiexe & Chr(34) & " " & Chr(34) & PERCminiopt & Chr(34) & " " & Chr(34) & PERCconfigfile & Chr(34) & " " & Chr(34) & Monitor & Chr(34) & " " & Chr(34) & Entity & Chr(34)& " "  & Chr(34) & Host & Chr(34) & " " & Chr(34) & Service & Chr(34) & " " & Chr(34) & Alias  & Chr(34) & " " & Chr(34) & xDate & Chr(34) & " "  & Chr(34) & xTime & Chr(34) & " " & Chr(34) & State & Chr(34) & " " & Chr(34) & Text & Chr(34), 0, True
Set WshShell = Nothing

 

Example program with parameters running directly from cmd window

mini.exe -f config.txt MIS_Monitor HR_Admin myserver HR DEV52 3/5/2002 10:22:12 ALERT "This is a test firing event from cmd line“

 

 

December 13th, 2011 3:40am

An approach as below would be far more manageable:

sCmd="C:\Perc\mini.exe"
aParm(0)="-f"
aParm(1)="C:\Perc\config.txt"
aParm(2)="MIS_Monitor"
aParm(3)="HR_Admin"
aParm(4)=" myserver "
aParm(5)="HR"
aParm(6)="DEV52"
aParm(7)=FormatDateTime(Date() ,2)
aParm(8)=FormatDateTime(Now() ,4)
aParm(9)="ALERT"
aParm(10) = "This is a test firing event from WMI using vbs"

DQ = """"
sParms = ""
For i = 0 To UBound(aParm)
    sParms = sParms & " " & DQ & aParm(i) & DQ
Next

Set oWshShell = CreateObject("WScript.Shell")
oWshShell.run sCmd & sParms, 1, True

Free Windows Admin Tool Kit Click here and download it now
December 13th, 2011 7:54am

A "Dim aParam(10)" statement is needed.  Otherwise, this approach throws a "type mismatch" error at line 2.

Also, I like to use a Join() instead of a for loop - just a preference ...

Dim aParm(10)
sCmd="C:\Perc\mini.exe"
aParm(0)="-f"
aParm(1)="C:\Perc\config.txt"
aParm(2)="MIS_Monitor"
aParm(3)="HR_Admin"
aParm(4)=" myserver "
aParm(5)="HR"
aParm(6)="DEV52"
aParm(7)=FormatDateTime(Date() ,2)
aParm(8)=FormatDateTime(Now() ,4)
aParm(9)="ALERT"
aParm(10) = "This is a test firing event from WMI using vbs"

DQ = """"
sParms = ""
oWshShell.Run DQ & sCmd & " " & DQ & Join(aparm, DQ & " " & DQ) & DQ, 1, True

It results in a command line like this ...

"C:\Perc\mini.exe" "-f" "C:\Perc\config.txt" "MIS_Monitor" "HR_Admin" " myserver " "HR" "DEV52" "12/13/2011" "08:47" "ALERT" "This is a test firing event from WMI using vbs"

I do note that the OP posted a working line without all of the double quotes.  Just the last argument was quoted.  However, I also see that he wrote the VBS part to quote everything.  Of course, we have no way of knowing if there is a functional difference between the two approaches.

December 13th, 2011 2:02pm

Thank you this solution was nice, only needed the Dim aParm(10).

Also to Tom's point I only needed the DQ around the last parameter event though DQ is OK as well.  If I only want the last parameter DQ can slightly change the script to

Dim aParm(10)

sCmd="C:\Perc\mini.exe"

aParm(0)="-f"

aParm(1)="C:\Perc\config.txt"

aParm(2)="MIS_Monitor"

aParm(3)="HCM_Admin"

aParm(4)=" server.com "

aParm(5)="HR"

aParm(6)="DEV52"

aParm(7)=FormatDateTime(Date() ,2)

aParm(8)=FormatDateTime(Now() ,4)

aParm(9)="ALERT"

aParm(10) ="""This is a test firing event from WMI using vbs"""


DQ = """"

sParms = ""

For i = 0 To UBound(aParm) 

    sParms = sParms & " " & aParm(i)

Next


wscript.echo sCmd & sParms

Set oWshShell = CreateObject("WScript.Shell")

oWshShell.run sCmd & sParms, 1, True


The biggest trick here is how to put double quotes in the string with 2 extra sets on each side (Total 6)

aParm(10) ="""This is a test firing event from WMI using vbs"""


  • Marked as answer by agianni2 Tuesday, December 13, 2011 4:54 PM
  • Edited by agianni2 Tuesday, December 13, 2011 5:06 PM
Free Windows Admin Tool Kit Click here and download it now
December 13th, 2011 4:53pm

Thanks Tom, learned a few things.  Your Solution works great per the original spec to DQ all parameters.  As you had a hunch this was not really required so I modified you solution slightly.

 

Dim aParm(10)

sCmd="C:\Perc\mini.exe"

aParm(0)="-f"

aParm(1)="C:\Perc\config.txt"

aParm(2)="MIS_Monitor"

aParm(3)="HR_Admin"

aParm(4)=" myserver.com "

aParm(5)="HR"

aParm(6)="DEV52"

aParm(7)=FormatDateTime(Date() ,2)

aParm(8)=FormatDateTime(Now() ,4)

aParm(9)="ALERT"

aParm(10) ="""This is a test firing event from WMI using vbs"""



wscript.echo sCmd & " " & Join(aparm, " " )



Set oWshShell = CreateObject("WScript.Shell")

oWshShell.Run sCmd & " " & Join(aparm, " " ), 1, True

Again biggest trick here is treating the double quotes

aParm(10) ="""This is a test firing event from WMI using vbs"""

  • Edited by agianni2 Tuesday, December 13, 2011 5:03 PM
  • Marked as answer by agianni2 Tuesday, December 13, 2011 5:03 PM
December 13th, 2011 5:01pm

Hi,

I would like to create a scheduled task with multiple parameters and run in shell. I have tried various ways and even followed your steps but I am thinking I am making some mistake. Could you help me out in executing the below command in vbScript shell?

"SCHTASKS.exe /CREATE /SC MONTHLY /MO FOURTH /D SUN /TN" &ArgVM& " /TR powershell -file C:\Praveen\Scripts\XenserverPS\ProdTasks\"&ArgVM&".ps1 -ExecutionPolicy Unrestricted /ST 04:30 /RU domain\user /RP password/RL HIGHEST"

Thanks

Praveen

Free Windows Admin Tool Kit Click here and download it now
May 25th, 2015 9:25am

I would like to create a scheduled task with multiple parameters and run in shell.

The question in this thread was answered four years ago. Please start a thread of your own. Since your question is related to schtasks.exe rather than to scripting (=programming),this forum would be a better place to post it.
May 25th, 2015 10:09am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics