WMI scripting template that runs the script against the computer whose name was included as a command-line argument. This script pings the remote machine before trying to make a connection. To run this script, type the name of the script followed by the name of the computer you want the script to run against. For example: my_script.vbs atl-ws-01.

Visual Basic
Edit|Remove
On Error Resume Next

If Wscript.Arguments.Count = 0 Then
    Wscript.Echo "You must enter the computer name when starting this script."
    Wscript.Quit
End If

strComputer = Wscript.Arguments.Item(0)

Set objShell = CreateObject("WScript.Shell")
strCommand = "%comspec% /c ping -n 3 -w 1000 " & strComputer & ""
Set objExecObject = objShell.Exec(strCommand)

Do While Not objExecObject.StdOut.AtEndOfStream
    strText = objExecObject.StdOut.ReadAll()
    If Instr(strText, "Reply") > 0 Then

        ' =====================================================================
        ' Insert your code here
        ' =====================================================================

        Set objWMIService = GetObject _
            ("winmgmts:\\" & strComputer & "\root\cimv2")
        Set colItems = objWMIService.ExecQuery _
            ("Select * From Win32_OperatingSystem")
        For Each objItem in ColItems
            Wscript.Echo strComputer & ": " & objItem.Caption
        Next

        ' =====================================================================
        ' End
        ' =====================================================================

    Else
        Wscript.Echo strComputer & " could not be reached."
    End If
Loop