DJ VenGenCe Posted December 23, 2005 Share Posted December 23, 2005 (edited) #Include <Constants.au3> Func RemoteReboot() $wbemFlagReturnImmediately = 0x10 $wbemFlagForwardOnly = 0x20 $colItems = "" $strComputer = inputbox( "Please enter the name of the computer you want to reboot", "Input" ) $objWMIService = ObjGet("winmgmts:{(RemoteShutdown)}//" & $strComputer & "\root\CIMV2") $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem", "WQL", _ $wbemFlagReturnImmediately + $wbemFlagForwardOnly) For $objOS in $colItems $objOS.Reboot() Next endFunc Call the Function and Whoolah! Putting this into a small program I am developing for IT/IS Staff where I work.. Thought I'd share Tested in 3.1.1+ Not tested on 2k or older systems. Sucessfully rebooted several machines on the domain. I find it useful if someone's machine is not responding (In some cases, WMI is still working) But not tested in every scenario yet. Comments, Suggestions? I want to add Force Logoff and Force Shutdown to it as well. Edited December 23, 2005 by DJ VenGenCe Link to comment Share on other sites More sharing options...
Moderators big_daddy Posted December 25, 2005 Moderators Share Posted December 25, 2005 This program here has those features, maybe you could us it to figure out how to add them to AutoIT.BeyondExec Link to comment Share on other sites More sharing options...
DJ VenGenCe Posted December 27, 2005 Author Share Posted December 27, 2005 Thx for the info Big Daddy, like most people, I like to figure things out... More than likely that program uses the same code itself, but has a neater interface than my simple code... Anyone have any other codes to reboot & or shutdown machine? and even force a log off? Link to comment Share on other sites More sharing options...
RagnaroktA Posted December 27, 2005 Share Posted December 27, 2005 DJ, Below is rough code to reboot, shutdown, log off, and force all three. It has been tested. The only thing I would suggest, is error correction. #Include <Constants.au3> $s_Machine = InputBox("Input", "Please enter the name of the computer you want to reboot") $s_Command = InputBox("Input", "0=Log Off|0+4=Forced Log Off|1=Shutdown|1+4=Forced Shutdown|2=Reboot|2+4=Forced Reboot|8=Power Off|8+4=Forced Power Off") _RemoteShutdown($s_Machine, $s_Command) Func _RemoteShutdown(ByRef $s_Machine, ByRef $s_Command) Local $wbemFlagReturnImmediately, $wbemFlagForwardOnly, $objWMIService, $colItems, $objItem $wbemFlagReturnImmediately = 0x10 $wbemFlagForwardOnly = 0x20 $colItems = "" $objWMIService = ObjGet("winmgmts:\\" & $s_Machine & "\root\CIMV2") $colItems = $objWMIService.ExecQuery("SELECT Name From Win32_OperatingSystem", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly) For $objItem in $colItems $objItem.Win32ShutDown($s_Command); 0=Log Off|0+4=Forced Log Off|1=Shutdown|1+4=Forced Shutdown|2=Reboot|2+4=Forced Reboot|8=Power Off|8+4=Forced Power Off Next EndFunc; ==> _RemoteShutdown Current Projects:Remote Administration Suite Updated! 12-20-07Remote User State Migration Tool (Plugin) Updated! 12-20-07Batch Print Wizard Updated! 12-20-07Links:AutoIt Beta | AutoIt Wiki Link to comment Share on other sites More sharing options...
DJ VenGenCe Posted December 28, 2005 Author Share Posted December 28, 2005 Thanks RagnaroktA, I have this code in an HTA that I was using before I discovered AutoITScript. Thanks for your help. Now how would error return work, I don't think it would generate and @Error number would it? Link to comment Share on other sites More sharing options...
RagnaroktA Posted December 28, 2005 Share Posted December 28, 2005 (edited) Thanks RagnaroktA, I have this code in an HTA that I was using before I discovered AutoITScript. Thanks for your help. Now how would error return work, I don't think it would generate and @Error number would it? I would add the following right after ObjGet("winmgmts... If @error Then MsgBox(16, "_RemoteShutdown", "ObjGet Error: winmgmts") Return EndIf Also, after objWMIService.ExecQuery If @error Then MsgBox(16, "_RemoteShutdown", "ExecQuery Error: SELECT Name FROM Win32_OperatingSystem") Return EndIf The only reason the script will error (apart from not being able to connect to the remote computer) will be if the variables given to it are incorrect. You will probably want to code something to say If $s_Machine = "" Or $s_Command = "" Then ; Throw Error Msgbox and exit function Endif The way that I have this code set up in my GUI, there's no way it will return blank or incorrect variables. That should really be all you need to worry about. Maybe have a function ping the target machine to make sure it's online. Edit: to answer your question... It shouldnt need to return a specific error number. If it passes, error will be 0. So if error <> 0, there was a problem somewhere. Edited December 28, 2005 by RagnaroktA Current Projects:Remote Administration Suite Updated! 12-20-07Remote User State Migration Tool (Plugin) Updated! 12-20-07Batch Print Wizard Updated! 12-20-07Links:AutoIt Beta | AutoIt Wiki Link to comment Share on other sites More sharing options...
Rahul Rohela Posted January 17, 2006 Share Posted January 17, 2006 DJ, Below is rough code to reboot, shutdown, log off, and force all three. It has been tested. The only thing I would suggest, is error correction. #Include <Constants.au3> $s_Machine = InputBox("Input", "Please enter the name of the computer you want to reboot") $s_Command = InputBox("Input", "0=Log Off|0+4=Forced Log Off|1=Shutdown|1+4=Forced Shutdown|2=Reboot|2+4=Forced Reboot|8=Power Off|8+4=Forced Power Off") _RemoteShutdown($s_Machine, $s_Command) Func _RemoteShutdown(ByRef $s_Machine, ByRef $s_Command) Local $wbemFlagReturnImmediately, $wbemFlagForwardOnly, $objWMIService, $colItems, $objItem $wbemFlagReturnImmediately = 0x10 $wbemFlagForwardOnly = 0x20 $colItems = "" $objWMIService = ObjGet("winmgmts:\\" & $s_Machine & "\root\CIMV2") $colItems = $objWMIService.ExecQuery("SELECT Name From Win32_OperatingSystem", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly) For $objItem in $colItems $objItem.Win32ShutDown($s_Command); 0=Log Off|0+4=Forced Log Off|1=Shutdown|1+4=Forced Shutdown|2=Reboot|2+4=Forced Reboot|8=Power Off|8+4=Forced Power Off Next EndFunc; ==> _RemoteShutdown Options like 0+4=Forced Log , 1+4=Forced Shutdown, 2+4=Forced Reboot, 8+4=Forced Power Off are not working else is fine... Please again check these options Link to comment Share on other sites More sharing options...
RagnaroktA Posted January 17, 2006 Share Posted January 17, 2006 Options like 0+4=Forced Log , 1+4=Forced Shutdown, 2+4=Forced Reboot, 8+4=Forced Power Off are not working else is fine...Please again check these optionsIt should work just fine. It's currently being used in the Administration Tool, and doesnt have problems. The only difference there is, I'm passing it a hardcoded variable. Instead of prompting for "0+4" if you select "Force Log Off" it will pass "0+4". The only reason this shouldnt work is if the function is being passed something it doesnt recognize.Make sure that you're putting in "0+4" and not the sum of this. (4) The same goes for the force of other options.Scott Current Projects:Remote Administration Suite Updated! 12-20-07Remote User State Migration Tool (Plugin) Updated! 12-20-07Batch Print Wizard Updated! 12-20-07Links:AutoIt Beta | AutoIt Wiki Link to comment Share on other sites More sharing options...
Rahul Rohela Posted January 21, 2006 Share Posted January 21, 2006 I am attaching VBScript for the same.. Convert it to autoit & see. expandcollapse popupfunction Ping(byval strName) dim objFSO, objShell, objTempFile, objTS dim sCommand, sReadLine dim bReturn set objShell = WScript.CreateObject("Wscript.Shell") set objFSO = CreateObject("Scripting.FileSystemObject") 'Set default return value bReturn = false 'Create command line to ping and save results to a temp file sCommand = "cmd /c ping.exe -n 3 -w 1000 " & strName & " > C:\temp.txt" 'Execute the command objShell.run sCommand, 0, true 'Get the temp file set objTempFile = objFSO.GetFile("C:\temp.txt") set objTS = objTempFile.OpenAsTextStream(1) 'Loop through the temp file to see if "reply from" is found, 'if it is then the ping was successful do while objTs.AtEndOfStream <> true sReadLine = objTs.ReadLine if instr(lcase(sReadLine), "reply from") > 0 then bReturn = true exit do end if loop 'Close temp file and release objects objTS.close objTempFile.delete set objTS = nothing set objTempFile = nothing set objShell = nothing set objFSO = nothing 'Return value Ping = bReturn end function '/\/\/\/\/\/\/\/\/\/\/\/\/\/\ end function '/\/\/\/\/\/\/\/\/\/\/\ Start Main body of script 'Get computer name to operate on ComputerName=InputBox("Enter the Machine name of the computer" & vbCRLF _ & "you wish to Shutdown / Reboot / Logoff", _ "Remote Shutdown / Reboot / Logoff", _ "ComputerName") 'if Cancel selected - exit If (ComputerName = "") Then Wscript.Quit 'change the name to uppercase ComputerName=UCase(ComputerName) 'ping the computername to see if it is accessible bPingtest = ping(Computername) If bPingtest = FALSE Then y = msgbox ("'" & ComputerName & "' is not accessible!" & vbCRLF _ & "It may be offline or turned off." & vbCRLF _ & "Check the name for a typo." & vbCRLF, _ vbCritical, ComputerName & " NOT RESPONDING") Wscript.Quit end IF 'Get the action desired Action=InputBox( _ "Select Action to perform on " & ComputerName & vbCRLF & vbCRLF _ & " 1 - Logoff" & vbCRLF _ & " 2 - Force Logoff ( NO SAVE )" & vbCRLF _ & " 3 - Powerdown" & vbCRLF _ & " 4 - Force Powerdown ( NO SAVE )" & vbCRLF _ & " 5 - Reboot" & vbCRLF _ & " 6 - Force Reboot ( NO SAVE )" & vbCRLF & vbCRLF _ & "NOTE:" & vbCRLF _ & " Using Force will close windows" & vbCRLF _ & " without saving changes!", _ "Select action to perform on " & ComputerName, "") 'if Cancel selected - exit If (Action = "") Then Wscript.Quit 'error check input If (INSTR("1234567",Action)=0) OR (Len(Action)>1) then y = msgbox("Unacceptable input passed -- '" & Action & "'", _ vbOKOnly + vbCritical, "That was SOME bad input!") Wscript.Quit end if 'set flag to disallow action unless proper input achieved, 1 => go 0 => nogo flag = 0 'set variables according to computername and action Select Case Action Case 1 'Logoff x = 0 strAction = "Logoff sent to " & ComputerName flag = 1 Case 2 'Force Logoff x = 4 strAction = "Force Logoff sent to " & ComputerName flag = 1 Case 3 'Powerdown x = 8 strAction = "Powerdown sent to " & ComputerName flag = 1 Case 4 'Force Powerdown x = 12 strAction = "Force Powerdown sent to " & ComputerName flag = 1 Case 5 'Reboot x = 2 strAction = "Reboot sent to " & ComputerName flag = 1 Case 6 'Force Reboot x = 6 strAction = "Force Reboot sent to " & ComputerName flag = 1 Case 7 'Test dialog boxes y = msgbox("Test complete", vbOKOnly + vbInformation, "Dialog Box Test Complete") flag = 0 Case Else 'Default -- should never happen y = msgbox("Error occurred in passing parameters." _ & vbCRLF & " Passed '" & Action & "'", _ vbOKOnly + vbCritical, "PARAMETER ERROR") flag = 0 End Select 'check flag ' if equal 1 (TRUE) then perform Win32Shutdown action on remote PC ' and display a confirmation message ' if not equal 1 (FALSE) then skip the action and script ends if flag then Set OpSysSet=GetObject("winmgmts:{(Debug,RemoteShutdown)}//" _ & ComputerName & "/root/cimv2").ExecQuery( _ "Select * from Win32_OperatingSystem where Primary=true") for each OpSys in OpSysSet OpSys.Win32Shutdown(x) y = msgbox(strAction,vbOKOnly + vbInformation,"Mission Accomplished") next end If 'Release objects set OpSys = nothing set OpSysSet = nothing 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