2013-07-31 101 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」)不需要括號 –

相關問題