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
Adding a Timeout?
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.
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.
Adding a Timeout?
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
uploads/7804/code_to.txt
Adding a Timeout?
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.
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.
Adding a Timeout?
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)
jvierra2009-11-23 12:10:11
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)
Adding a Timeout?
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
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
Adding a Timeout?
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.
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.