Cortesía de Torgeir Bakken [MVP]
'you should ping first, because the WMI
'connection time-out is *long* if the computer is offline
strComputer = "something"
' ping the computer to see if it is online
If IsConnectible(strComputer, "", "") Then
' error handling the connection to strComputer
On Error Resume Next
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "oot\cimv2")
If Err.Number = 0 Then
On Error Goto 0
' Do your WMI handling here
Else
WScript.Echo sCompName & " is online but not available"
End If
Else
WScript.Echo sCompName & " is not online"
End If
Function IsConnectible(sHost, iPings, iTO)
' Returns True or False based on the output from ping.exe
'
' Author: Alex Angelopoulos/Torgeir Bakken
' Works an "all" WSH versions
' sHost is a hostname or IP
' iPings is number of ping attempts
' iTO is timeout in milliseconds
' if values are set to "", then defaults below used
If iPings = "" Then iPings = 2
If iTO = "" Then iTO = 750
Const OpenAsASCII = 0
Const FailIfNotExist = 0
Const ForReading = 1
Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
sTempFile = sTemp & "unresult.tmp"
oShell.Run "%comspec% /c ping.exe -n " & iPings & " -w " & iTO _
& " " & sHost & ">" & sTempFile, 0 , True
Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, _
FailIfNotExist, OpenAsASCII)
sResults = fFile.ReadAll
fFile.Close
oFSO.DeleteFile(sTempFile)
Select Case InStr(sResults,"TTL=")
Case 0 IsConnectible = False
Case Else IsConnectible = True
End Select
End Function
Saludos
Ramon Jimenez
Microsoft Services
Leer las respuestas