Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to Run an Executable

Status
Not open for further replies.

nevets2001uk

IS-IT--Management
Jun 26, 2002
609
GB
I'm trying to script the running of MBSA so that I can schedule automatic monthly scans of our servers. I have a script that creates an output directory but when I try to run the executable with the required switches it fails.

I'm using the line

wshshell.run "%COMSPEC% /k ""C:\Program Files\Microsoft Baseline Security Analyzer 2\mbsacli.exe"" /target HUKEA1692-ici\HGBEAA01 /wa /nvc /qp /qt /rd ""\\HGBEAC02\Private\Information Technology Department\Administration\Logs""",1,true

The path the executable and the output directory for MBSA reports need to be in quotes to function (due to spaces in the path) and I though the double quotes should do the job. However in the cmd window that opens I get the error 'C:\Program' is not recognised as an internal or external command.

If I remove the output path section of the string it runs fine but won't save the log where I want it.

Any thoughts as this is bugging me now?


Steve G (MCSE / MCSA:Messaging)
 
Put that commandline into a batch file and run the batch file, say path:\abc.bat, with %comspec% /k path\abc.bat. The reason is not that your commandline has anything wrong, it is because it is too long, coming closing or surpassing 255 characters after expansion of %comspec%. (The exact length limit is around that number but shorter than 255 - can search the documentation if you're interested in the exact figure.) Or you can use 8.3 directory/file convention to make it shorter - I think in this case it would just about to be short enough.
 
Thanks for your suggestion. My only problem is that I want to end up with something like

wshshell.run "%COMSPEC% /k ""C:\Program Files\Microsoft Baseline Security Analyzer\mbsacli.exe"" /target HUKEA1692-ici\HGBEAA01 /wa /nvc /qp /qt /rd ""\\HGBEAC02\Private\Information Technology Department\Administration\Logs\" & year & "\" & month & """",1,true

so that the output goes to a folder that the code creates using the current date. Such as ...Logs\2008\09 - September

Can this be done inside the batch? Or can the generated command string be passed to a batch file?

Steve G (MCSE / MCSA:Messaging)
 
[0] The problem is solely technical. The wshshell.run() accepts only commandline with certain length limitation. This is often happening if you run, say, java with quite a massive classpath to be specified (just an example). Hence, a way around the limit is to put that line in a batch file and run it with wshshell.run().

[1] Suppose you run the script by double click on a .vbs You can make a .bat in the same directory of the .vbs and use scriptfullpath of the .vbs to make sure you dynamically find the .bat.
[tt]
bat_name="abc.bat" 'the batch file name: as given
bat_dir=replace(wscript.scriptfullname,wscript.scriptname,"") 'except contrived case, it should be enough
bat_fullname=bat_dir & bat_name

arg_y=year(date)
arg_m=month(date)

scmd="%comspec% /k " & chr(34) & bat_fullname & chr(34) & & " " & arg_y & " " & arg_m
set wshshell=createobject("wscript.shell")
wshshell.run scmd,1,true
set wshshell=nothing
[/tt]
[2] And then the batch file (abc.bat) contains something like this.
[tt]
@echo off
"C:\Program Files\Microsoft Baseline Security Analyzer\mbsacli.exe" /target HUKEA1692-ici\HGBEAA01 /wa /nvc /qp /qt /rd "\\HGBEAC02\Private\Information Technology Department\Administration\Logs\[red]%1[/red]\[red]%2[/red]"
[/tt]
 
I have a typo (doubl &) there. The corresponding line should be read like this.
[tt] scmd="%comspec% /k " & chr(34) & bat_fullname & chr(34) [highlight]&[/highlight] " " & arg_y & " " & arg_m
[/tt]
 
Thanks that seems to be getting me there. A final question is that in the second argument the folder name has a space so I enclosed it in quotes. However when it's read inside the batch it still contains the quote marks and therefore causes the path to be incorrect.

How can you pass a string with spaces into the batch without the quote marks being interpretted by the batch?

Steve G (MCSE / MCSA:Messaging)
 
But you don't need to remove the quote from the batch file. Why isn't it done in the vbs?
 
Here's another idea to shorten the path strings. test for unassigned drive letters then use the SUBST command. e.g. (assuming J: and K: are free to use)
Code:
subst J: "C:\Program Files\Microsoft Baseline Security Analyzer 2"
subst K: "\\HGBEAC02\Private\Information Technology Department\Administration\Logs"
...which would mean your code shortens to something like:
Code:
wshshell.run "%COMSPEC% /k J:\mbsacli.exe"" /target HUKEA1692-ici\HGBEAA01 /wa /nvc /qp /qt /rd ""K:\" & year & "\" & month & """",1,true
I've not tested this for properly paired-off quote marks, but I'm sure you get the idea. I think it also has the happy result of making the command-line code a little easier to read.

None of this in any way negates Tsuji's suggestions of course, it's just another approach that should achieve the same affect.

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, and so on)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top