从命令

时间:2017-10-23 11:26:40

标签: vba excel-vba shell excel

我有一些问题要从Shell命令中捕获结果。这是我的代码:

Shell "runas.exe /user:technician ""C:\Work\test.bat""", vbNormalFocus
Application.Wait (Now + TimeValue("0:00:03"))
SendKeys pass & "{ENTER}"
Sheets("File").Cells(1, 1) = Shell 'Here to paste the result

我的" test.bat"文件包含" PING' ComputerName'"仅用于测试目的,因为我有其他复杂的命令,运行10秒。代码如下(每​​个用户检测Domain中的ProxyServer):

reg query "\\PECPC021\HKU\S-1-5-21-3026626878-399171236-2381696588-4970\software\microsoft\windows\currentversion\internet settings" /s | find /i "ProxyServer"

以下命令可以捕获数据,但不能使用" runas.exe"功能,因为我需要与" SendKeys"和"等待"功能粘贴密码。

strShellCommand = "runas.exe /user:technician ""C:\Work\test.bat"""
Set oSh = CreateObject("WScript.Shell")
Set oEx = oSh.Exec(strShellCommand) 'command can be combine in the "exec" Function?
strbuf = oEx.stdOut.ReadAll
Sheets("File").Cells(1, 1) = strbuf

0 个答案:

没有答案