swampman Posted August 30, 2008 Share Posted August 30, 2008 This is my first "big" script using Autoit and I am getting the following error when the code runs and cannot figure out what to do. The script is suppose to read a input file of hostnames and then find out how long it has been since they rebooted, and if it is over the set amount of time reboot. The script works for the first name in the file but then the error comes on the second name. #include <Date.au3> #include <File.au3> $wbemFlagReturnImmediately = 0x10 $wbemFlagForwardOnly = 0x20 $colItems = "" Global $sfile = "hostnames.txt", $strComputer If Not _FileReadToArray($sfile, $strComputer) Then MsgBox(4096,"Error", " Error reading file hostnames to Array error:" & @error) Exit EndIf For $x = 1 to $strComputer[0] $objWMIService = ObjGet("winmgmts:\\" & $strComputer[$x] & "\root\CIMV2") $colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_OperatingSystem", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly) If IsObj($colItems) Then For $objItem In $colItems $LastBootUp = WMIDateStringToDate($objItem.LastBootUpTime) $LocalDateTime = WMIDateStringToDate($objItem.LocalDateTime) IF _DateDiff("h", $LastBootUp, $LocalDateTime) > 24 Then Restart() ;Execute Function Restart() EndIf MsgBox(0,"",$strComputer[$x]) Next Else MsgBox(0, "WMI Output", "No WMI Objects Found for class: " & "Win32_OperatingSystem") EndIf Next ;************************************************************* ;* Function WMIDateStringToDate ;************************************************************* Func WMIDateStringToDate($dtmDate) Return (StringMid($dtmDate, 1, 4) & "/" & _ StringMid($dtmDate, 5, 2) & "/" & _ StringMid($dtmDate, 7, 2) & _ " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate, 13, 2)) EndFunc ;==>WMIDateStringToDate ;************************************************************* ;* Function Restart() - Forces Reboot of System ;************************************************************* Func Restart() Shutdown(6) EndFunc The error: Line 27 (File "C:\Documents and Settings\Dan\Desktop\Uptime2.au3"): $colItems=$objWMIService.ExecQuery (Select * From Win32_OperatingSystem", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardonly) $colitems = $objWMIService^ERROR Error: Variable must be of type "Object". Link to comment Share on other sites More sharing options...
Andreik Posted August 30, 2008 Share Posted August 30, 2008 (edited) This line is problem: $objWMIService = ObjGet("winmgmts:\\" & $strComputer[$x] & "\root\CIMV2") Look if you read exactly what you want from file. If I replace $strComputer[$x] with . the script work fine so I think that what you read from file in $strComputer array is the problem. Edited August 30, 2008 by Andreik When the words fail... music speaks. Link to comment Share on other sites More sharing options...
PsaltyDS Posted August 30, 2008 Share Posted August 30, 2008 (edited) This line is problem: $objWMIService = ObjGet("winmgmts:\\" & $strComputer[$x] & "\root\CIMV2") Look if you read exactly what you want from file. If I replace $strComputer[$x] with . the script work fine so I think that what you read from file in $strComputer array is the problem. Even if the computer name is correct, the connection might fail (lack of perms, box is not on, etc.). So test the object before using it: For $x = 1 To $strComputer[0] $objWMIService = ObjGet("winmgmts:\\" & $strComputer[$x] & "\root\CIMV2") If IsObj($objWMIService) Then $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem", "WQL", _ $wbemFlagReturnImmediately + $wbemFlagForwardOnly) If IsObj($colItems) Then For $objItem In $colItems $LastBootUp = WMIDateStringToDate($objItem.LastBootUpTime) $LocalDateTime = WMIDateStringToDate($objItem.LocalDateTime) If _DateDiff("h", $LastBootUp, $LocalDateTime) > 24 Then Restart();Execute Function Restart() EndIf MsgBox(0, "", $strComputer[$x]) Next Else MsgBox(0, "WMI Output", "No WMI Objects Found for class: " & "Win32_OperatingSystem") EndIf Else MsgBox(16, "Error", "Failed to connect to WMI service on: " & $strComputer[$x]) EndIf Next This was already being done for the collection object, just apply it to the service object also. Edited August 30, 2008 by PsaltyDS Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
swampman Posted August 31, 2008 Author Share Posted August 31, 2008 Thanks for the help. The remote computer is a Vista machine and I have found after some research that Vista can have issue's with remote WMI Connecting to WMI on a Remote Computer. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now