VBScript Capture StdOut from ShellExecute

2

I am trying to run the following code snippet as part of a tool to gather and log some pertinent system diagnostics. The purpose of this snippet is to gather the result of running the command:

vssadmin list writers

The snippet is as follows:

'   Set WshShell = CreateObject("WScript.Shell")
'   WScript.Echo sCurPath & "\vsswritercheck.bat"
'   Set WshShellExec = WshShell.Exec("elevate.cmd cmd.exe /c " & sCurPath & "\vsswritercheck.bat")

Set oShell = CreateObject("Shell.Application")
oShell.ShellExecute "cmd.exe", sCurPath & "\vsswritercheck.bat", , "runas", 1
vsswriter = VSSWriterCheck

Select Case oShell.Status
    Case WshFinished
        strOutput = oShell.StdOut.ReadAll
    Case WshFailed
        strOutput = oShell.StdErr.ReadAll
End Select
WScript.Echo strOutPut
vsswriter = strOutPut

With the first code snippet (commented out) I can run the command and capture stdout from the batch file. In the second code snipped, I cannot capture stdout.

I need to be able to run the batch script with Elevated permissions, so I am looking for a compromise between the functionality of the two.

I cannot run the entire calling script in elevated mode due to restrictions from other pieces of functionality.

I am looking for any ideas on how to add this output to my log as I am running out of options that are within the scope of basic scripts.

Joe

Posted 2012-09-24T14:37:42.180

Reputation: 73

Answers

1

strcmd="cmd /c " & sCurPath & "\vsswritercheck.bat"
return = wshshell.run(strcmd , 0 , true)
if return=0 then
    blnSuccess = True
else
    blnSuccess = False
end if

José Luís Teixeira

Posted 2012-09-24T14:37:42.180

Reputation: 26

It would be nice if you could provide a bit of context to go with the code :) – Der Hochstapler – 2012-09-26T15:27:28.043

I think that is almost what I need, but I need to capture StdOut from the script, not just if it was successful or not. – Joe – 2012-10-01T13:05:11.853

0

How about using them both?

Use that code you commented out, which works in non-elevated mode, and add an extra test where, if elevated rights are needed, the script will instead call itself using ShellExecute(), causing the subsequent call to Exec() to already have elevated rights and still capture stdout.

It's a bit wacky, but effortless.

Crafty

Posted 2012-09-24T14:37:42.180

Reputation: 1