2017-08-07 215 views
-1

我是vbscript的新手。我試圖從下面的PingTest函數獲取返回值,但它在函數定義中給我一個錯誤(我正在使用Windows 10)。使用VBScript從函數返回值

Function PingTest(hostName) 
    ' Standard housekeeping 
    Dim colPingResults, objPingResult, strQuery 

    ' Define the WMI query 
    strQuery = "SELECT * FROM Win32_PingStatus WHERE Address = '" & hostName & "'" 

    ' Run the WMI query 
    colPingResults = GetObject("winmgmts://./root/cimv2").ExecQuery(strQuery) 

    ' Translate the query results to either True or False 
    For Each objPingResult In colPingResults 
     If Not IsObject(objPingResult) Then 
      PingTest = False 
     ElseIf objPingResult.StatusCode = 0 Then 
      PingTest = True 
     Else 
      PingTest = False 
     End If 
    Next 
    colPingResults = Nothing 
End Function 


Dim output 
output= PingTest("www.google.com") 

WScript.Echo output 
+0

你說錯了?哪一個? – JNevill

+0

它說「Line1:Char 31 Expected statement」 –

+0

修改後的代碼有效! –

回答

3

指定函數的返回類型如

Function PingTest(hostName) as Boolean 

illegal in VBscript

關於第二個想法:

類型化變暗 - 在

Dim output As Boolean = PingTest("www.google.com") 

不是VBScript中,無論是。並將對象分配到colPingResults需要Set

+0

謝謝我糾正它,它的工作 –