Adding a Timeout?

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 14 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
new_user
Posts: 157
Last visit: Tue May 06, 2014 5:46 pm

Adding a Timeout?

Post by new_user »

The below piece of code works great now that I added some additional output [thanks jhicks]. The question I have is 99% of the time this works fine. I am running this against this one domain and it seems to get stuck on one machine or something and not move on to the next, i.e. like its stuck retrieving data because if the machine is not online or I do not have permissions its echo's the error and moves along. Given my code can I find what machine its stuck on and also add a timeout so it would echo the machine name after the timeout and just move along? Thanks in advance.

uploads/7804/code_to.txt
User avatar
new_user
Posts: 157
Last visit: Tue May 06, 2014 5:46 pm

Adding a Timeout?

Post by new_user »

The below piece of code works great now that I added some additional output [thanks jhicks]. The question I have is 99% of the time this works fine. I am running this against this one domain and it seems to get stuck on one machine or something and not move on to the next, i.e. like its stuck retrieving data because if the machine is not online or I do not have permissions its echo's the error and moves along. Given my code can I find what machine its stuck on and also add a timeout so it would echo the machine name after the timeout and just move along? Thanks in advance.

uploads/7804/code_to.txt
User avatar
rasimmer
Posts: 182
Last visit: Fri Apr 25, 2014 7:00 am

Adding a Timeout?

Post by rasimmer »

I have a couple of suggestions, firstly, this piece of code:

Do Until objRecordSet.EOF strComputer=objRecordSet.Fields("Name").Value set objWMIDateTime = CreateObject("WbemScripting.SWbemDateTime") set objWMI = GetObject("winmgmts:" & strComputer & "rootcimv2") If Err.Number <> 0 Then Wscript.Echo strComputer & ";" & objRecordSet.Fields("distinguishedName").Value & ";" & Err.DescriptionErr.ClearElse'wscript.echo strcomputer
I think this line should be placed with object creation like objConnection and objCommand, there is no need to continually re-create the object every time you process a computer:

set objWMIDateTime = CreateObject("WbemScripting.SWbemDateTime")
Next, your On Error Resume Next should be local to where the error occurs, not global.

On Error Resume Next
set objWMI = GetObject("winmgmts:" & strComputer & "rootcimv2") If Err.Number <> 0 Then Wscript.Echo strComputer & ";" & objRecordSet.Fields("distinguishedName").Value & ";" & Err.Description On Error Goto 0
Else On Error Goto 0
'wscript.echo strcomputer
Lastly, as your seeing, timeout for WMI is time consuming. So, to minimize it you could do two things. Ping the computer:

If blnPing(strComputer) Then
'Computer PING successful
Else
'Computer PING fail
End if

Function blnPing(device_name) Dim colItems, objItem, objWMIPing, propValue On Error Resume Next Set objWMIPing = GetObject("winmgmts:.rootcimv2") Set colItems = objWMIPing.ExecQuery("Select * from Win32_PingStatus Where Address = '" & device_name & "'") blnPing = "" For Each objItem in colItems If objItem.StatusCode = 0 Then blnPing = True 'strIP = objItem.ProtocolAddress Else blnPing = False End If Next On Error GoTo 0End Function

And, if you wanted could even do a test to verify the trust relationship is working by doing a simple file creation on the remote computer with FSO to let you know that you have access to the box and can get to it. I would create a boolean function for this as well. Once those two steps are completed, then attempt a WMI RPC call.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Adding a Timeout?

Post by jvierra »

The simple answer is - fix the broken machine. Use WMI Diag tool to troubleshoot faikng machine.

Log the connections to a file. The one that is last in the file is the failed machine.

The maximum timeout for a WMI query is 2 minutes. If it fails to return after two minutes then the target machine has WMI issues.


Here is the manual method in case your timeout is not working. The follwong sets teh connection to timeout at max (2 minutes). A zero setting set no timeout (wait forever)

Code: Select all

	
server = "localhost" 
namespace = "rootcimv2" 
UserName = ""
Password = ""
'iSecFlag 0 '= forever 
iSecFlag = 128 '= (2 minutes) wbemConnectFlagUseMaxWait
	

set loc = CreateObject("WbemScripting.SWbemLocator" ) 
Set WBemServices = loc.ConnectServer(Server, Namespace,UserName,password,,,iSecFlag) 
	
jvierra2009-11-23 12:10:11
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Adding a Timeout?

Post by jvierra »

Two numbers - flags. Or rather just one flag set or reset.

FLag 128 set is max wait
flag 128 reset wait forever.

jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Adding a Timeout?

Post by jvierra »

Then you know it's the next machine after the one in the log file so you have the answer.

Put thhe code where you connect WMI to the machine.

You need to change the query executor to

Set collection = WBemServices.ExecQuery( ...query... ) jvierra2009-11-23 13:09:56
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Adding a Timeout?

Post by jvierra »

Just change your WMI connection to use the locator service. The example is complete just sub in the correct variables.

Add a log entry to output the machine name before attempting the connection. This will give you a trace in the log file in case machines hang completely.



This topic is 14 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked