Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Limit time to make a connection to a remote compter using WMI

Status
Not open for further replies.

josephk73

Technical User
Joined
Aug 1, 2002
Messages
115
Location
GB
All, the code snippet below is pretty standard and familiar to most of you, for connecting to a local/remote computer:

Set objWMIService=GetObject("winmgmts:\\" & StrServer & "\root\cimv2")

My problem is that if a computer is offline or if I cannot authenticate to it, about 1 min elapses (as it tries to connect or authenticate) before it gives up and moves onto the next machine in my list.

Obviously this slows the whole script down which is a real pain.

My question - is there a way that you can limit the time to make a connection to the remote computer? So if a connection cant be made in say 12 seconds then the script moves onto the next computer in the list?

Any help would be great.

Cheers
 
I also had the same problem, and created a little function to see if I could ping the system first...

Code:
Function TestConnection(strComputer)
	'Description:	Pings the given address using WMI
	'Returns:	Boolean True or False if a connection was found or not
	'Attention!	This is supported from winxp/server2003 up
	Dim objPing, objStatus
	Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
	ExecQuery("select * from Win32_PingStatus where address = '"_
	& strComputer & "'")
	For Each objStatus in objPing
		If IsNull(objStatus.StatusCode) Or objStatus.StatusCode <> 0 Then TestConnection = False
		If objStatus.StatusCode = 0 Then TestConnection = True
	Next
End Function

Please tell me if I'm wrong I like to learn from my mistakes...
_____________________________________
Feed a man a fish and feed him for a day.
Teach a man to fish and feed him for a lifetime...
 
K0b3

Thanks for this. Seems to work just fine.

Cheers.
 

Here's one that works works with anything that responds to a ping.

'IsOnline.vbs
'************************************* Samples
'Call IsOnline("192.168.0.102")
'Call IsOnline("mickeymouse")

If IsOnline("mickeymouse") then
wscript.echo "Doing something based on true responce 2"
Else
wscript.echo "Doing something based on false responce 2"
End If
wscript.quit

'********************************************************************************************
' Function returns true if lstrcomputer is online, does ping test for reply in console message
Function IsOnline(lstrComputer)
'add next line to main program for repeated use
Set objShell = CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec ("%comspec% /c ping -n 3 -w 25 " & lstrComputer)
Do While Not objExecObject.StdOut.AtEndOfStream
strText = objExecObject.StdOut.ReadAll()
If Instr(strText, "Reply") > 0 Then
IsOnline=True
Else
IsOnline=False
End If
Loop
End Function
'********************************************************************************************
 
The script I posted also works for everything that responds to a ping... This is because you are using WMI on the machine where the script is initiated to do the ping request... It doesn't connect to the remote machine using WMI...

Please tell me if I'm wrong I like to learn from my mistakes...
_____________________________________
Feed a man a fish and feed him for a day.
Teach a man to fish and feed him for a lifetime...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top