显示基于ping成功/失败的消息的脚本

时间:2013-07-31 14:27:13

标签: vbscript

现在我有以下脚本工作:

set WshShell = CreateObject("WScript.Shell") 

WshShell.run ("%COMSPEC% /c ipconfig  /release"), 0, true
WshShell.run ("%COMSPEC% /c ipconfig  /renew"), 0, true

PINGFlag = Not CBool(WshShell.run("ping -n 1 www.google.com",0,True))
If PINGFlag = True Then
    MsgBox("ip release/renew was successful")
Else
    MsgBox("ip release/renew was not successful")
End If

我对vbscript并不熟悉,但它似乎是显示弹出消息的最佳选择。我把这个脚本和我在网上找到的其他人拼凑在一起,所以我想知道它的工作原理:

我的问题是,我不明白以下行是如何工作的:

PINGFlag = Not CBool(WshShell.run("ping -n 1 www.google.com",0,True))

如何确定PINGFlag的布尔值?

谢谢!

1 个答案:

答案 0 :(得分:2)

.Run(...)返回已执行进程的退出代码/错误级别。对于其他数字,CBool​​()返回False为0或True。通过应用Not,'good'的情况变为True,所有'bad'错误级别都为False。

然后你可以自然地编写If语句:

If PINGFlag Then ' comparing boolean variables against boolean literals is just noise
    MsgBox "ip release/renew was successful" ' no () when calling a sub
Else
    MsgBox "ip release/renew was not successful"
End If