使用VBScript从函数返回值
问题描述:
我是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
答
指定函数的返回类型如
Function PingTest(hostName) as Boolean
关于第二个想法:
类型化变暗 - 在
Dim output As Boolean = PingTest("www.google.com")
不是VBScript中,无论是。并将对象分配到colPingResults
需要Set
。
+0
谢谢我纠正它,它的工作 –
你说错了?哪一个? – JNevill
它说“Line1:Char 31 Expected statement” –
修改后的代码有效! –