2015-03-31 79 views
0

我想檢查我在vbscript中運行的netstat命令的輸出是否爲空。由於端口沒有被使用,下面的工作不起作用,但是它跳過了這個事實並進入了ERROR(else)。檢查輸出是否爲空

我認爲這是由於IsNULL?我無法找到我可以在vbscript中使用的其他功能嗎?

Set netStatRun = objShell.Exec("cmd /C ""netStat -ano |find ""1002""""") 
     netStatOutPut = netStatRun.StdOut.ReadLine 
     WScript.Echo "The value is: " & netStatOutPut 

      If IsNull(netStatOutPut) Then 
       WScript.Echo "The port is free" 
      Else 
       WScript.Echo "ERROR! Port is use" 
      End If 

OUTPUT:

The value is: 
ERROR! Port is use 

回答

2

使用使用Len(netStatOutPut) = 0

If Len(netStatOutPut) = 0 Then 
    WScript.Echo "The port is free" 
Else 
    WScript.Echo "ERROR! Port is use" 
End If 

IsNull(netStatOutPut)試試看你能不能用IsNull確定一個字符串是零長度字符串代替。

+0

完美!非常感謝 - 效果很好。 – lara400 2015-03-31 14:19:45

0
If netstatOutput = "" Then 
    WScript.Echo "OK: port is free" 
Else 
    WScript.Echo "ERROR: port is in use" 
End If