【发布时间】:2014-03-25 11:52:41
【问题描述】:
我正在尝试让一些 VBA 代码工作以返回计算机 Ping 结果、安装日期和 BIOS 版本(以获取日期)。
For i = 1 To lRow
Debug.Print i
addr1 = ActiveSheet.Cells(i, 1).Value 'this contains my IP/Computer Name
cmdPing = "C:\Windows\System32\PING.EXE " & addr1
Set shPing = CreateObject("Wscript.shell")
Set runPing = shPing.exec(cmdPing)
strPing = runPing.stdout.readall
ActiveSheet.Cells(i, 2).Value = strPing
Set runPing = Nothing
Set shPing = Nothing
cmdInstall = "systeminfo /s " & addr1 & " | findstr /C:""Install Date"""
Set shInstall = CreateObject("Wscript.shell")
Set runInstall = shInstall.exec(cmdInstall)
strInstall = runInstall.stdout.readall
ActiveSheet.Cells(i, 3).Value = strInstall
Set runInstall = Nothing
Set shInstall = Nothing
cmdBios = "systeminfo /s " & addr1 & " | findstr /C:""BIOS Version"""
Set shBios = CreateObject("Wscript.shell")
Set runBios = shBios.exec(cmdBios)
strBios = runBios.stdout.readall
ActiveSheet.Cells(i, 4).Value = strBios
Set runBios = Nothing
Set shBios = Nothing
Next i
Ping 工作正常,但 systeminfo 短暂闪烁并消失。我可以只用"systeminfo /s " & addr1 运行,但显然这会带来很多不必要的信息。我感觉这与传递" 字符有关。也试过/C:" & Chr(34) & "也无济于事。
【问题讨论】:
-
是的
ASC("|") = CHR(124)但这不是问题。我不确定为什么它不起作用,但你可以做的是只运行一次"systeminfo /s " & addr1,然后使用 VBA 从返回的字符串中提取安装日期和 Bios 版本 -
是的,我知道我可以从完整结果中提取信息(这是我这次运行它时所做的),但我想弄清楚如何让它在未来像这样工作,因为事实是它应该工作;)
-
对,所以如果你运行
Debug.Print runInstall.stderr.readall它实际上会显示一个错误ERROR: Invalid argument/option - '|'. Type "SYSTEMINFO /?" for usage.我认为由于某种原因findstr没有被识别 -
然后我找到了答案here
-
啊哈!不会想到事先调用 cmd 。很好的发现!