我正在尝试编写一个脚本来 ping IP 地址,直到收到响应。当它执行时,它会启动另一个名为“sound.vbs”的脚本。我有 2 个问题:
- 我不 希望执行 ping 命令时弹出 cmd 窗口。
- 即使 ping 失败,脚本也会直接关闭,而不是等待一段时间并重试 ping。
代码:
Dim objShell
Set objShell = Wscript.CreateObject("WScript.Shell")
Dim target 'define target ip
Dim result 'define ping result
target= "193.105.173.130" 'Archeage EU server IP (possibly Shatigon)
result = "Request timed out" 'Initial result
Set shell = WScript.CreateObject("WScript.Shell") 'create WScript shell
Set shellexec = shell.Exec("ping " & target) 'setting up the ping
Dim count
count = 1
Do
result = LCase(shellexec.StdOut.ReadAll)
If InStr(result , "reply from") Then
objShell.Run "sound.vbs"
Set objShell = Nothing
count = count + 1
Else
WScript.Sleep 4000
End If
Loop until count < 2
如何解决列出的问题?
最佳答案
你可以尝试像这个脚本一样修改你的:
Option Explicit
Dim strComputer,objPing,objStatus
strComputer = "smtp.gmail.com"
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}!\\").ExecQuery _
("select * from Win32_PingStatus where address = '" & strComputer & "'")
For Each objStatus in objPing
If objStatus.Statuscode = 0 Then
Call MyProgram()
wscript.quit
End If
Next
'****************************************************
Sub MyProgram()
Dim objShell
Set objShell = CreateObject( "WScript.Shell" )
objShell.Run("calc.exe")
Set objShell = Nothing
End Sub
'****************************************************
灵感来自Loop a function?
关于shell - Ping 和启动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32540459/