VBScript 從 ShellExecute 擷取標準輸出

VBScript 從 ShellExecute 擷取標準輸出

我正在嘗試運行以下程式碼片段作為收集和記錄一些相關係統診斷資訊的工具的一部分。此程式碼片段的目的是收集運行命令的結果:

vssadmin list writers

片段如下:

'   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

透過第一個程式碼片段(已註解掉),我可以運行命令並從批次檔中捕獲標準輸出。在第二個程式碼片段中,我無法捕獲標準輸出。

我需要能夠以提升的權限運行批次腳本,因此我正在尋找兩者功能之間的折衷方案。

由於其他功能的限制,我無法在提升模式下運行整個呼叫腳本。

我正在尋找有關如何將此輸出添加到我的日誌中的任何想法,因為我已經用完了基本腳本範圍內的選項。

答案1

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

答案2

兩者都使用怎麼樣?

使用您註解掉的程式碼(程式碼在非提升模式下運作),並新增一個額外的測試,如果需要提升權限,腳本將改為使用ShellExecute() 呼叫自身,從而導致對Exec() 的後續呼叫已經具有提升權限並且仍然捕獲標準輸出。

這有點古怪,但毫不費力。

相關內容