Search This Blog

Wednesday, June 18, 2008

Computer Inventory Script With INI Control

I made a network inventory script that collects machine information such as installed applications, mapped drives and printers, network information, and creates backups of the workstations. 

So far it has proven successful in my parent company, where I used a hard coded exe with no INI file version. 

I have since ported it to use an INI file. You can create different INI files based on the department the program will be used for and launch the program as inventory.exe billing.ini

Everything is pretty much self explanatory. Let me know if you have any questions. This was made on 06/20/2007

#include <Array.au3>
#NoTrayIcon
;AutoItSetOption("MustDeclareVars", 1)
 
Dim $INI
Dim $INI_RunApplications, $INI_RunPrinters, $INI_RunDrives, $INI_RunBackup, $INI_RunNetwork
Dim $INI_FileInventory, $INI_FileBKSFile, $INI_FileBKFFile, $INI_FileBackupLogs
Dim $INI_ExitLetter, $INI_ExitFile
Dim $INI_ExcludeAppsDisp[100]
Dim $INI_ExcludeAppsPub[100]
Dim $INI_SortDisplayName, $INI_SortPublisher, $INI_SortDescending
Dim $INI_CommentsApps[3]
Dim $INI_CommentsPrint[3]
Dim $INI_CommentsDrives[3]
Dim $INI_CommentsNetwork[3]
Dim $INI_BackStartMsgLine[10], $INI_BackEndMsgLine[10]
Dim $INI_BackStartMsgEnabled, $INI_BackStartMsgCloseDelay, $INI_BackEndMsgEnabled, $INI_BackEndMsgCloseDelay
Dim $INI_BackupLogType, $key, $INI_BackupHide
Dim $DisplayName
Dim $Publisher
Dim $AppIncrement ;Array increment to be used as a counter and argument passed to the AppWrite function.
Dim $A_Apps[1000]    ;Array to be used for collection of data
Dim $A_Prn[25]
Dim $A_Drv[25]
$ObjNet = ObjCreate("WScript.NetWork") 
 
$INI = $CmdLine[$CmdLine[0]]
If $INI = "" Then $INI = "Inventory.ini" 
If FileExists($INI) = 0 Then Exit 
 
 
Call ("INI_Load")
FileDelete ($INI_FileInventory)
FileOpen ($INI_FileInventory, 10)
If $INI_RunApplications =1 then Call("Applications")
If $INI_RunPrinters = 1 Then Call("Printers")
If $INI_RunDrives = 1 then call("Drives")
If $INI_RunNetwork = 1 Then call ("Network")
If $INI_RunBackup = 1 Then call ("Backup")
FileClose ($INI_FileInventory) ;Close the Inventory file.
 
Func INI_Load() ;INI Load 
;SECTIONS_TO_RUN 
    $INI_RunApplications = IniRead($INI, "SECTIONS_TO_RUN", "Applications",1)
    $INI_RunPrinters = IniRead($INI, "SECTIONS_TO_RUN", "Printers",1)
    $INI_RunDrives = IniRead($INI, "SECTIONS_TO_RUN", "Drives",1)
    $INI_RunNetwork = IniRead($INI, "SECTIONS_TO_RUN", "Network",1)
    $INI_RunBackup = IniRead($INI, "SECTIONS_TO_RUN", "Backup",1)
 
;FILE 
    $INI_FileInventory = IniRead($INI, "FILE", "Inventory","") & "\" & @UserName & "\inventory.txt"
    
    $INI_FileBKSFile = IniRead($INI, "FILE", "BKSFile", "backup.bks")
    $INI_FileBKFFile = IniRead($INI, "FILE", "BKFFile", "inventory.bkf")
    $INI_FileBackupLogs = IniRead($INI,"FILE", "BackupLogs",0)
 
;EXIT_ON_COMPUTERNAME
    For $i = 1 to StringLen(@Computername)
        $INI_ExitLetter = IniRead($INI, "EXIT_ON_COMPUTERNAME", "Letter" & $i,"")
        If $INI_ExitLetter <> "" and $INI_ExitLetter = StringMid (@Computername, $i,1) Then Exit
    Next
 
;EXIT_ON_PRESENT_FILE
    For $i = 1 to 5
        $INI_ExitFile = IniRead($INI, "EXIT_ON_PRESENT_FILE", "File" & $i, "")
        If $INI_ExitFile <> "" and FileExists($INI_ExitFile) Then Exit
    Next
 
;EXCLUDE_APPLICATIONS
    for $i = 1 to 100
        if IniRead($INI, "EXCLUDE_APPLICATIONS", "DisplayName" & $i,"") <> "" then _ArrayAdd ($INI_ExcludeAppsDisp, IniRead($INI, "EXCLUDE_APPLICATIONS", "DisplayName" & $i,""))
        if IniRead($INI, "EXCLUDE_APPLICATIONS", "Publisher" & $i,"") <> "" then _ArrayAdd ($INI_ExcludeAppsPub, IniRead($INI, "EXCLUDE_APPLICATIONS", "Publisher" & $i,""))
    Next
    _ArraySort($INI_ExcludeAppsDisp)
    _ArraySort($INI_ExcludeAppsPub)
 
;COMMENTS
    For $i = 1 to 3
        _ArrayInsert($INI_CommentsApps, $i, IniRead($INI, "COMMENTS", "Applications" & $i, "*****Applications*****"))
        _ArrayInsert ($INI_CommentsPrint, $i, IniRead ($INI, "COMMENTS", "Printers" & $i, "*****Printers*****"))
        _ArrayInsert ($INI_CommentsDrives, $i, IniRead ($INI, "COMMENTS", "Drives" & $i, "*****Drives*****"))
        _ArrayInsert ($INI_CommentsNetwork, $i, IniRead ($INI, "COMMENTS", "Network" & $i, "*****Network Info*****"))
    Next
 
;BACKUP START/END MESSAGE
    $INI_BackStartMsgEnabled = IniRead($INI, "BACKUP_START_MESSAGE", "Enabled", 0)
    $INI_BackStartMsgCloseDelay = IniRead($INI, "BACKUP_START_MESSAGE", "CloseDelay", 20)
    $INI_BackEndMsgEnabled = IniRead($INI, "BACKUP_END_MESSAGE", "Enabled", 0)
    $INI_BackEndMsgCloseDelay = IniRead($INI, "BACKUP_END_MESSAGE", "CloseDelay", 20)
    
    For $i = 1 to 10
        _ArrayInsert ($INI_BackStartMsgLine, $i, IniRead($INI, "BACKUP_START_MESSAGE", "Line" & $i, ""))
        _ArrayInsert ($INI_BackEndMsgLine, $i, IniRead($INI, "BACKUP_END_MESSAGE", "Line" & $i, ""))
    Next
 
;BACKUP
    $INI_BackupLogType = IniRead($INI, "BACKUP", "BackupLogType", "F")
    $INI_BackupHide = IniRead($INI, "BACKUP", "BackupHide", 1)
EndFunc
 
Func Applications() ;--------------------Applications--------------------
    Filewrite ($INI_FileInventory, @CRLF)
    For $i = 1 to 3
        FileWrite ($INI_FileInventory, $INI_CommentsApps[$i] & @CRLF)
    Next
    FileWrite ($INI_FileInventory, "DisplayName" & @TAB & "Publisher" & @CRLF)
    FileWrite ($INI_FileInventory, "----------" & @TAB & "----------" & @CRLF)
    ;Enumerate applitions under HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall key in the registry. Up to 1000 keys.
    
    For $AppIncrement = 1 to 1000 
        $key = RegEnumKey ("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\", $AppIncrement) 
        if @error <> 0 then ExitLoop
        $DisplayName = RegRead ("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & $key, "DisplayName")
        $Publisher = RegRead ("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & $key, "Publisher")
        If $DisplayName <> $Publisher and $DisplayName <> "" Then
            _ArrayInsert ($A_Apps, $AppIncrement, $DisplayName & @TAB & $Publisher & @CRLF)
        EndIf
        Sleep(10)
    Next
    
    _ArraySort ($A_Apps,0,0,1000) ;Sort the array by $DisplayName, or whatever the first variable value in the begging of the line is. - INI
    
    for $i = 1 to UBound($INI_ExcludeAppsDisp,1)-1
        $Pos = _ArraySearch ($A_Apps,$INI_ExcludeAppsDisp[$i],0,0,0,true)
        if $Pos <> -1 then 
            _ArrayDelete($A_Apps, $Pos)
            $i = $i - 1
        EndIf
    Next
    _ArraySort($A_Apps)
    for $i = 1 to UBound($INI_ExcludeAppsPub,1)-1
        $Pos = _ArraySearch ($A_Apps,$INI_ExcludeAppsPub[$i],0,0,0,true)
 
        if $Pos <> -1 then 
            _ArrayDelete($A_Apps, $Pos)
            MsgBox(0,$INI_ExcludeAppsPub[$i],$Pos)
            $i = $i - 1
        EndIf
    Next
 
    _ArraySort ($A_Apps,0,0,1000)
    FileWrite($INI_FileInventory, _ArrayToString($A_Apps, "", 0, UBound($A_Apps,1) -1)) ;Convert the $A_apps array to a string and write the contents to a file.
 
EndFunc
 
func Printers()
    Filewrite ($INI_FileInventory, @CRLF)
    For $i = 1 to 3
        FileWrite ($INI_FileInventory, $INI_CommentsPrint[$i] & @CRLF)
    Next
    
    $ObjPrn = $ObjNet.EnumPrinterConnections
    FileWrite ($INI_FileInventory, "Port" & @TAB & "Printer" & @CRLF)
    FileWrite ($INI_FileInventory, "----------" & @TAB & "----------" & @CRLF)
    For $i = 0 to $ObjPrn.Count -1
        _ArrayInsert($A_Prn, $i, $ObjPrn.Item($i))
    Next
    For $i = 0 to 25
        If $A_Prn[$i] = "" then ExitLoop
        FileWrite ($INI_FileInventory, $A_Prn[$i] & @TAB & $A_Prn[$i + 1] & @CRLF)
        $i = $i + 1
    Next
EndFunc
 
Func Drives()
    Filewrite ($INI_FileInventory, @CRLF)
    For $i = 1 to 3
        FileWrite ($INI_FileInventory, $INI_CommentsDrives[$i] & @CRLF)
    Next
    $ObjDrv = $ObjNet.EnumNetworkDrives
    FileWrite ($INI_FileInventory, "Drive" & @TAB & "Path" & @CRLF)
    FileWrite ($INI_FileInventory, "----------" & @TAB & "----------" & @CRLF)
    For $i = 0 to $ObjDrv.Count -1
        _ArrayInsert ($A_Drv, $i, $ObjDrv.Item($i))
    Next
    For $i = 0 to 25
        If $A_Drv[$i] = "" then ExitLoop
        FileWrite ($INI_FileInventory, $A_Drv[$i] & @TAB & $A_Drv[$i + 1] & @CRLF)
        $i = $i + 1
    Next
EndFunc
 
Func Network()
    Filewrite ($INI_FileInventory, @CRLF)
        For $i = 1 to 3
        FileWrite ($INI_FileInventory, $INI_CommentsNetwork[$i] & @CRLF)
    Next
    FileWrite ($INI_FileInventory, "Computer Name: " & @Tab & @ComputerName & @CRLF)
    FileWrite ($INI_FileInventory, "Username: " & @TAB & @UserName& @CRLF)
    FileWrite ($INI_FileInventory, "Operating System: " & @TAB & @OSVersion & " / " & @OSServicePack & @CRLF)
 
    $strComputer = "."
    $objWMIService = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
    $nics = $objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 
    For $nic in $nics 
        FileWrite ($INI_FileInventory, "Description:" & @TAB & $nic.Description & @CRLF)
        if $nic.DHCPEnabled = True then FileWrite ($INI_FileInventory, "DHCP Enabled" & @CRLF)
        For $i = 0 To UBound($nics)    ;log IP/SubnetMask
            if $nic.IPAddress($i) <> 0 Then FileWrite ($INI_FileInventory, "IPAddress" & $i & ":" & @TAB & $nic.IPAddress($i) & "/" & $nic.IPSubnet($i) & @CRLF)
            If $nic.DefaultIPGateway($i) <> 0 then FileWrite ($INI_FileInventory, "Gateway" & $i & ":" & @TAB & $nic.DefaultIPGateway($i) & @CRLF)
            If $nic.DNSServerSearchOrder($i) <> 0 then FileWrite ($INI_FileInventory, "DNS" & $i & ":" & @TAB & $nic.DNSServerSearchOrder($i) & @CRLF)
        Next
        FileWrite ($INI_FileInventory, @CRLF)    
        
    Next
EndFunc
 
Func Backup()
    if $INI_BackStartMsgEnabled <> 0 Then MsgBox (262144,"PLEASE READ", _ArrayToString($INI_BackStartMsgLine, @CRLF), $INI_BackStartMsgCloseDelay)
    $NTBACKUP = 'ntbackup.exe' & ' backup "@' & $INI_FileBKSFile & '" /j "Backup" /f "' & $INI_FileBKFFile & "\" & @username & '\Backup.bkf" /A /V:yes /L:' & $INI_BackupLogType & ' /M normal /SNAP:off'
 
    ;hide the backup utility
    Select
        Case $INI_BackupHide = 1
            Run ($NTBACKUP , @SystemDir);run the backup utility
            for $i = 1 to 60
                WinSetState ("Backup Progress", "", @SW_HIDE)
                WinSetState ("Backup Utility - [Untitled]", "", @SW_HIDE)
                WinSetState ("Selection Information", "", @SW_HIDE)
                $i =  $i + 1
                sleep (1000)
            next
            
        Case $INI_BackupHide = 0
            RunWait ($NTBACKUP , @SystemDir);run the backup utility
            
    EndSelect
 
    $BackupState = WinGetState ("Backup Progress", "")
    $BackupUtility = WinGetState ("Backup Utility - [Untitled]")
 
    ;check if the backup is running
    While $BackupState <> 0 
        ;& $BackupUtility <> 0
        Sleep (10000)
        $BackupState = WinGetState ("Backup Progress", "")
        $BackupUtility = WinGetState ("Backup Utility - [Untitled]")
    WEnd
 
    ;Copy over the backup logs
    FileCopy ('c:\Documents and Settings\' & @UserName & '\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\*.*', $INI_FileBackupLogs & "\" & @UserName)
    if $INI_BackEndMsgEnabled <> 0 Then MsgBox(262144,"BACKUP COMPLETE", _ArrayToString($INI_BackEndMsgLine, @CRLF), $INI_BackEndMsgCloseDelay)
 
 
EndFunc
 
 

Inventory.ini:

; This INI file is for the Inventory script created by Alex Belenkiy ab@nyitsupport.com
; Do not modify any of the sections or keys of this file, only the values
 
[SECTIONS_TO_RUN] 
;Set 1 to run and 0 to skip
Applications=1
Printers=1
Drives=1
Network=1
Backup=0
 
[FILE]
;Set the path and filename of the inventory and backup files. Use just the filename to create in the program start folder.
;Inventory="\\Server\Share" will result it \\Server\Share\USERNAME\inventory.txt
Inventory=c:\dev
BKSFile=c:\DEV\test.bks
BKFFile=C:\DEV
BackupLogs=C:\DEV
 
[EXIT_ON_COMPUTERNAME]
;Exit the program if a character is present in the computername. To exclude all computers with 
;a second letter dash (ex. J-Doe) use Letter2=-
;To exclude all computers with a 3rd letter D and 5th letter e use Letter3=D Letter11=e
Letter1=
Letter2=
Letter3=
Letter4=
Letter5=
Letter6=
Letter7=
Letter8=
Letter9=
Letter10=
Letter11=
Letter12=
Letter13=
Letter14=
Letter15=
Letter16=
Letter17=
Letter18=
Letter19=
Letter20=
 
[EXIT_ON_PRESENT_FILE]
;Exit the application if a file is present, such as the previously performed inventory or backup.
;Use full path and file name
File1=
File2=
File3=
File4=
File5=
 
[EXCLUDE_APPLICATIONS]
;Application inventory involves checking the HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall key
;To skip applications from being logged, enter their DisplayName or Publisher values,
;excatly as written under the corresponding registry values.
DisplayName1="Ad-Aware"
DisplayName2="Adobe Acrobat"
DisplayName3="Adobe Flash"
DisplayName4="Adobe Reader"
DisplayName5="ATI"
DisplayName6="Blackberry"
DisplayName7="Broadcom"
DisplayName8="Compaq"
DisplayName9="Dameware"
DisplayName10="eSafe eConsole"
DisplayName11="HighMAT"
DisplayName12="Hotfix for Windows"
DisplayName13="HP"
DisplayName14="Intel(r)"
DisplayName15="LiveUpdate"
DisplayName16="Macromedia"
DisplayName17="MetaFrame"
DisplayName18="Microsoft Base"
DisplayName19="Microsoft Data"
DisplayName20="Microsoft Easy"
DisplayName21="Microsoft Exchange"
DisplayName22="Microsoft Inter"
DisplayName23="Microsoft .Net"
DisplayName24="Microsoft Office Live"
DisplayName25="Microsoft Office Professional"
DisplayName26="Microsoft SQL"
DisplayName27="Microsoft User"
DisplayName28="Microsoft National"
DisplayName29="Microsoft Compression"
DisplayName30="Microsoft XML"
DisplayName31="Mozilla"
DisplayName32="OpenMG"
DisplayName33="RealPlayer"
DisplayName34="RecordNow"
DisplayName35="Roxio"
DisplayName36="Security Update"
DisplayName37="Sonic"
DisplayName38="Sony"
DisplayName39="SoundMax"
DisplayName40="Sun Microsystems"
DisplayName41="Symantec Antivirus"
DisplayName42="Track-IT"
DisplayName43="Update for Windows"
DisplayName44="WebEx"
DisplayName45="WebFldrs"
DisplayName46="Windows XP Hotfix"
DisplayName47="WinImage"
DisplayName48="WinZip"
DisplayName49="Yahoo"
DisplayName50="WinRAR archiver"
DisplayName51="Security Update for Windows XP"
DisplayName52="Adobe Download"
DisplayName53="InterVideo WinDVD"
DisplayName54="Windows Driver Package"
DisplayName55="Windows Media"
DisplayName56="Windows Rights"
DisplayName57=
DisplayName58=
DisplayName59=
DisplayName60=
DisplayName61=
DisplayName62=
DisplayName63=
DisplayName64=
DisplayName65=
DisplayName66=
DisplayName67=
DisplayName68=
DisplayName69=
DisplayName70=
DisplayName71=
DisplayName72=
DisplayName73=
DisplayName74=
DisplayName75=
DisplayName76=
DisplayName77=
DisplayName78=
DisplayName79=
DisplayName80=
DisplayName81=
DisplayName82=
DisplayName83=
DisplayName84=
DisplayName85=
DisplayName86=
DisplayName87=
DisplayName88=
DisplayName89=
DisplayName90=
DisplayName91=
DisplayName92=
DisplayName93=
DisplayName94=
DisplayName95=
DisplayName96=
DisplayName97=
DisplayName98=
DisplayName99=
DisplayName100=""
Publisher1="Apple"
Publisher2="Google"
Publisher3="Hewlett"
Publisher4="Intel"
Publisher5="InterVideo Inc."
Publisher6=
Publisher7=
Publisher8=
Publisher9=
Publisher10=
Publisher11=
Publisher12=
Publisher13=
Publisher14=
Publisher15=
Publisher16=
Publisher17=
Publisher18=
Publisher19=
Publisher20=
Publisher21=
Publisher22=
Publisher23=
Publisher24=
Publisher25=
Publisher26=
Publisher27=
Publisher28=
Publisher29=
Publisher30=
Publisher31=
Publisher32=
Publisher33=
Publisher34=
Publisher35=
Publisher36=
Publisher37=
Publisher38=
Publisher39=
Publisher40=
Publisher41=
Publisher42=
Publisher43=
Publisher44=
Publisher45=
Publisher46=
Publisher47=
Publisher48=
Publisher49=
Publisher50=
Publisher51=
Publisher52=
Publisher53=
Publisher54=
Publisher55=
Publisher56=
Publisher57=
Publisher58=
Publisher59=
Publisher60=
Publisher61=
Publisher62=
Publisher63=
Publisher64=
Publisher65=
Publisher66=
Publisher67=
Publisher68=
Publisher69=
Publisher70=
Publisher71=
Publisher72=
Publisher73=
Publisher74=
Publisher75=
Publisher76=
Publisher77=
Publisher78=
Publisher79=
Publisher80=
Publisher81=
Publisher82=
Publisher83=
Publisher84=
Publisher85=
Publisher86=
Publisher87=
Publisher88=
Publisher89=
Publisher90=
Publisher91=
Publisher92=
Publisher93=
Publisher94=
Publisher95=
Publisher96=
Publisher97=
Publisher98=
Publisher99=
Publisher100=
 
[COMMENTS]
Applications1="**************************************"
Applications2="**********   Applications   **********"
Applications3="**************************************"
Printers1=**************************************
Printers2=**********     Printers     **********
Printers3=**************************************
Drives1=**************************************
Drives2=**********      Drives      **********
Drives3=**************************************
Network1=**************************************
Network2=**********   Network Info   **********
Network3=**************************************
 
[BACKUP_START_MESSAGE]
;Message to display after inventory, but before backup. Set Enabled to 0 to disable.
;CloseDelay will close the message after a predetermined amount of time in seconds.
;The user can also click OK to close it earlier.
Enabled=1
CloseDelay=20
Line1=Your computer is currently being backed up by the IT department engineers.
Line2=Please do not shut down or restart your computer until the backup is complete
Line3=The backup may take up to 30 minutes, you will be notified upon completion
Line4=If you have any questions please call 212-555-1212.
Line5=You may continue to work with your applications as usual while the backup is in progress.
Line6=Please do not save or edit any more files on the 'Local C' drive after the backup job is complete.
Line7=
Line8=
Line9=
Line10=
 
[BACKUP_END_MESSAGE]
;Message to display after the backup is complete.
Enabled=1
CloseDelay=20
Line1=The Backup was complete
Line2=Thank you
Line3=
Line4=
Line5=
Line6=
Line7=
Line8=
Line9=
Line10=
 
[Backup]
BackupHide=1
BackupLogType=S
 

Desktop Shortcut Remapper - ShortCut

Use this script to change all desktop shortcuts users may have to another location.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_outfile=ShortCut.exe
#AutoIt3Wrapper_Compression=4
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
; Remap all shortcuts on the desktop from one server to another.
; Will check each shortcut for a keyword and replace with another
; To run just execute 
;           ShortCut.exe "\\server1\share1" "\\server2\share2"
;         or     ShortCut.exe server1 server2
;        or    ShortCut.exe share1 share2
;
; Copyright © Alex Belenkiy 2008
; ab@nyitsupport.com
; Last Update 06/18/08
 
#include <array.au3>
#include <file.au3>
 
 
if $CmdLine[0] <> 2 then Exit
 
dim $Links [50]
dim $newSC [7]
dim $from, $to
 
$from = $CmdLine[1]
$to = $CmdLine[2]
$Links = _FileListToArray(@DesktopDir, "*.lnk", 1)
            
for $a = 1 to $Links[0]
    $sc = FileGetShortcut(@DesktopDir & "\" & $Links[$a])
    if StringInStr($sc[0], $from) Then 
        for $b = 0 to 6
            $newSC[$b] = StringReplace($sc[$b], $from, $to)
 
        Next
        FileDelete(@DesktopDir & "\" & $Links[$a])
        FileCreateShortcut($newSC[0], @DesktopDir & "\" & StringReplace($Links[$a], $from, $to), $newSC[1], $newSC[2], $newSC[3], $newSC[4], "", $newSC[5], $newSC[6])
    EndIf
Next
 

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