Thursday, June 5, 2008

Scheduled Shutdown of Windows XP Machines

Here is a script that i made as an answer to a free solution to an automatic shutdown of domain pcs. Basically, you create a domain computer startup policy that will install this compiled script. (compile using autoitscript.com) Everything is pretty much self explanatory.
#NoTrayIcon
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_outfile=ShutDown.exe
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_UseAnsi=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
 
; This script will shut down a machine on a schedule.
; The system time will be checked against the schedule only as often as necessary. 
; If there are two hours to go, it will check back in two hours, etc...
; The sequence allows the code to run without checking the time every minute.
; The program should be launched as a domain computer startup policy as follows
; \\SERVER\SHARE\NYITSUPPORTShutDown.exe /install 2330 
; The time is specified in a 4 digit military time format (2330 = 11:30 PM)
; The time has to be listed after the /install switch.
; /uninstall will remove the application.
; Alex Belenkiy ab@nyitsupport.com
; 06/05/2008
 
AutoItSetOption ("RunErrorsFatal", 0)
AutoItSetOption ("TrayIconHide",1)
#include <Date.au3>
#include <array.au3>
 
Dim $InstallPath
if $CmdLine[0] = "" then Exit
 
$InstallPath = "C:\Program Files\NYITSUPPORT\ShutDown\"
 
Call ("StartSequence")
 
Func StartSequence()
    Select
        case $CmdLine [1] = "/uninstall"
            if not IsAdmin() then Exit
            if not FileExists($InstallPath) then Exit
            call ("Uninstall")
            
        case $CmdLine [1] = "/install" 
            if $CmdLine [2] = "" then Exit
            if not StringIsDigit ($CmdLine[2]) then Exit
            if stringlen($CmdLine[2]) <> 4 then Exit
            if StringLeft($CmdLine[2], 2) > 23 then Exit
            if StringRight($CmdLine[2], 2) >59 then Exit
            if not IsAdmin() then Exit
            if StringRight(RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "NYITSUPPORTShutDown"),4) = $CmdLine [2] then Exit
            Call ("Install")
            Exit
            
        case $CmdLine [1] = "/NYITSUPPORTShutDown"
            if $CmdLine [2] = "" then Exit
            if not StringIsDigit ($CmdLine[2]) then Exit
            if stringlen($CmdLine[2]) <> 4 then Exit
            if StringLeft($CmdLine[2], 2) > 23 then Exit
            if StringRight($CmdLine[2], 2) > 59 then Exit
            call ("NYITSUPPORTShutDown")
            
        case Else
            Exit
            
    EndSelect
EndFunc
 
Func Install()
    FileCopy (@ScriptFullPath, $InstallPath,8)
    RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run","NYITSUPPORTShutDown", "REG_SZ", '"' & $InstallPath & 'NYITSUPPORTShutDown.exe" /NYITSUPPORTShutDown ' & $CmdLine [2])
    $list = ProcessList("NYITSUPPORTShutDown.exe")
    for $i = 1 to $list[0][0]
        $proc = $list[$i][1]
        if $proc <> @AutoItPID then ProcessClose($proc)
    next
    ShellExecute($InstallPath & "NYITSUPPORTShutDown.exe", "/NYITSUPPORTShutDown " & $CmdLine[2])
    $log = FileOpen("\\rginyarchive\pc archive\NYITSUPPORTShutDown\Logs\" & @ComputerName & ".log",9)
    FileWriteLine($log, @CRLF & "Installed - " & @MON & "/" & @MDAY & "/" & @YEAR & " - " & @HOUR & ":" & @MIN)
    FileClose($log)
EndFunc
 
Func Uninstall()
    RegDelete("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "NYITSUPPORTShutDown")
 
    $list = ProcessList("NYITSUPPORTShutDown.exe")
    for $i = 1 to $list[0][0]
        $proc = $list[$i][1]
        if $proc <> @AutoItPID then ProcessClose($proc)
    next
    DirRemove($InstallPath,1)
    $log = FileOpen("\\rginyarchive\pc archive\NYITSUPPORTShutDown\Logs\" & @ComputerName & ".log",9)
    FileWriteLine($log, @CRLF & "Uninstalled - " & @MON & "/" & @MDAY & "/" & @YEAR & " - " & @HOUR & ":" & @MIN)
    FileClose($log)
EndFunc
 
Func NYITSUPPORTShutDown()
    $H = StringLeft($CmdLine[2],2)
    $M = StringRight($CmdLine[2],2)
    While 1
        if @HOUR > $H Then $Delay = (23 - (@hour - $H)) * 3600000
        if @HOUR < $H then $Delay = ($H - @HOUR) * 3600000
        if @HOUR = $H then 
            if @MIN > $M then $Delay = 23 * 3600000
            if @MIN < $M then $Delay = ($M - @MIN) * 60000
            if @MIN = $M then 
                MsgBox(48, "Scheduled System Shutdown In Progress","Please close all open applications and save your data." & @CRLF & "The system will shutdown in 5 minutes", 60)    
                sleep(300000)
                $log = FileOpen("\\rginyarchive\pc archive\NYITSUPPORTShutDown\Logs\" & @ComputerName & ".log",9)
                FileWriteLine($log, @CRLF & "NYITSUPPORTShutDown Executed - " & @MON & "/" & @MDAY & "/" & @YEAR & " - " & @HOUR & ":" & @MIN)
                FileClose($log)
                Shutdown(13)
                
            EndIf
        EndIf
        sleep ($Delay)
    WEnd
EndFunc

No comments: