2013-07-31 58 views
0

现在我有以下脚本工作:脚本显示基于平成功的消息/失败

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的布尔值的?

谢谢!

+0

查看关于使用Visual Studio调试WSH脚本我的笔记[这里](http://stackoverflow.com/a/13802548/111794) 。此外,请参阅[这里](http://msdn.microsoft.com/en-us/library/d5fk67ky(v = vs.84).aspx)关于'Run'方法。 –

回答

2

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

然后你就可以编写如果语句 '自然':

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 
+0

“(%COMSPEC%/ c ipconfig/release”)'或'(“%COMSPEC%/ c ipconfig/renew”)不需要括号 –

相关问题