Skip to content

Instantly share code, notes, and snippets.

@Carm01
Last active March 20, 2019 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Carm01/a6c72f0b4a99481e242fae11f255805a to your computer and use it in GitHub Desktop.
Save Carm01/a6c72f0b4a99481e242fae11f255805a to your computer and use it in GitHub Desktop.
Reboot multiple machines based off a list or reboot a single machine AutoIT script.
pc1
pc2
pc3
pc4
5
pc3
1reboot.txt - is a file that contains a list of all PC's that you want to reboot. If there are spaces before or after that computername; no worries as the script will strip them away.
Rebootmultiple.au3 - Is the AutoIT script designed to read the list of computers and send a reboot command to them.
The script also removes all empty lines from the list when running . The script removes duplicate names as well after it strips leading and trailing white spaces.(the file will not changes as this is done in memory).
The script is extra cautions and asks you twice if you want to really reboox X number of machines. You can see the Progress ( its really fast ) as I throw up a splash box to show which machin it is on. Then rens the forced restart command. That command will NOT give the end user a warning nor will it check for logged on users. It will not wait for open programs to terminate either.
There is a 100ms pause ( line 50) between each reboot in case your network admin throws a temper tantrum of what you are doing. You can increase this to a higher value if you want, but there is no need to.
RebootSingle.au3 - Another AutoIT script designed to reboot a single machine on a domain and verify if anyone is logged on or not. It uses the same force reboot command as the above script. This is designed to work on a domain. In line 21 place your domain name in there. DO NOT place the DOT or anything after it.
$YourFile = Get-Content 'C:\users\joe\desktop\computers.txt'
foreach ($computer in $YourFile)
{
Restart-Computer -ComputerName $computer -force
}
<#
https://community.spiceworks.com/topic/1981643-remote-restart-from-txt-file-listing-computers
#>
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile_x64=MultipleReBoot.exe
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_UseX64=y
#AutoIt3Wrapper_Res_Comment=Your comments here
#AutoIt3Wrapper_Res_Description=reboots machines from list file 1reboot.txt
#AutoIt3Wrapper_Res_Fileversion=1.0.0.0
#AutoIt3Wrapper_Res_ProductName=Multi-Reboot
#AutoIt3Wrapper_Res_ProductVersion=1.0.0.0
#AutoIt3Wrapper_Res_Language=1033
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#RequireAdmin
#include <MsgBoxConstants.au3>
#include <File.au3>
#include <Array.au3>
#include <Debug.au3>
Global $file, $count, $line, $cmd, $count, $numberPC
$file = @ScriptDir & "\1reboot.txt"
_FileReadToArray($file, $count)
For $i = UBound($count) - 1 To 1 Step -1
$count[$i] = StringStripWS($count[$i], 3) ; ; strips all spaces before and after computername nothing else
$count1 = Asc($count[$i]) ; 0 = null, 32 = spaces only
If $count1 = 0 Or $count1 = 32 Then
_ArrayDelete($count, $i) ; removes blanks or spaces only
EndIf
Next
$count = _ArrayUnique($count) ; removes duplicates
;_DebugArrayDisplay($count) ; displays the list if you want to see it first
$numberPC = UBound($count) - 1 ; the number in the list now
$MyBox = MsgBox(33, "GrassHopper Says:", "REROOT ALL " & $numberPC & " Computers on your list?")
If $MyBox == 2 Then
Exit
ElseIf $MyBox == 1 Then
EndIf
$MyBox = MsgBox(33, "GrassHopper Says:", "ARE YOU SURE YOU WANT TO REROOT ALL " & $numberPC & " computers")
If $MyBox == 2 Then
Exit
ElseIf $MyBox == 1 Then
Call("exe")
exit
EndIf
Func exe()
SplashTextOn("Current Machine", "", 120, 70, -1, -1, 22, "")
For $i = 1 To $numberPC
$line = $count[$i]
ControlSetText("Current Machine", "", "Static1", $line)
;ConsoleWrite($line & @CRLF)
Run("shutdown -r -t 0 -m \\" & $line, @SystemDir, @SW_HIDE)
Sleep(100)
Next
EndFunc ;==>exe
; IMPORTANT THAT YOUR DOMAIN NAME GOES THERE, NOTHING AFTER THE DOT
#RequireAdmin
$machinename1 = InputBox("ReBoot Single Machine", "What is the Hostname or IP address?", "IP Address or Hostname")
If @error = 1 Then
Exit
EndIf
$machinename = StringStripWS($machinename1, 3) ; strips leading and trailing white spaces
If $machinename = "IP Address or Hostname" Or $machinename = "" Then
MsgBox(16, "ERROR ID10T", "You Must Enter A Valid Hostname Noob!" & @CRLF & @CRLF & "Best Regards", 10) ; makes sure you put something in the field
ElseIf $machinename <> "" Then
$MyBox = MsgBox(4, "CONFIRM", "Are You Sure you want to reboot the following machine: " & @CRLF & @CRLF & $machinename) ; confirmation
If $MyBox == 6 Then
$iPing = "ping -n 2 " & $machinename
Global $we = (_getDOSOutput($iPing) & @CRLF)
; gets logged in user if any on a domain machine
If StringInStr($we, "time=") > 1 Or StringInStr($we, "time<") > 1 Then
$cmd = "wmic /node:" & '"' & $machinename & '"' & " ComputerSystem Get UserName"
Global $we = (_getDOSOutput($cmd) & @CRLF)
If StringInStr($we, "domain_name_here_without_the_dot") = 0 Then
$we = "No logged in user"
Else
$we1 = StringSplit($we, "\")
$we = $we1[2]
$Sdata = $machinename & "," & "Logged on user: " & StringStripWS($we, 3)
If $we = "No logged in user" Then
$Sdata = $we
EndIf
go()
EndIf
; continues to reboot if you do not cancel out in the go() subroutine
Run("shutdown -r -f -t 0 -m \\" & $machinename, @SystemDir, @SW_HIDE)
MsgBox(64, "Confirmation", "Reboot Command sent to: " & $machinename, 5)
Else
MsgBox(16, "ERROR", "Please verifiy the target " & $machinename & " is turned on", 10)
EndIf
ElseIf $MyBox == 7 Then
Exit
EndIf
ElseIf @error = 1 Then
Exit
ElseIf $machinename = "" Then
Exit
EndIf
Func _getDOSOutput($command)
Local $text = '', $Pid = Run('"' & @ComSpec & '" /c ' & $command, '', @SW_HIDE, 2 + 4)
While 1
$text &= StdoutRead($Pid, False, False)
If @error Then ExitLoop
Sleep(10)
WEnd
Return StringStripWS($text, 7)
EndFunc ;==>_getDOSOutput
Func go()
$MyBox = MsgBox(33, "GrassHopper Says:", "User " & $Sdata & " is logged onto this machine, are you sure you want to continue rebooting this machine?")
If $MyBox == 2 Then
Exit ; exits of you cancel
ElseIf $MyBox == 1 Then
;
EndIf
$MyBox = MsgBox(33, "GrassHopper Says:", "Contine rebooting with user " & $Sdata & " logged on?")
If $MyBox == 2 Then
Exit ; exits of you cancel
ElseIf $MyBox == 1 Then
;
EndIf
EndFunc ;==>go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment