-1

I am new to vbscript. I am trying to get return value from a PingTest function below but it gives me an error at the function definition (I am using windows 10).

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
moooni moon
  • 333
  • 1
  • 5
  • 19

1 Answers1

3

Specifying the return type of the function as in

Function PingTest( hostName ) as Boolean

is illegal in VBscript.

On second thought:

Typed Dims - as in

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

aren't VBScript, either. And assigning objects to colPingResults needs Set.

Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96