Jump to content

Recommended Posts

Hello,

I am %*&%*+$ with the Windows Updates every 2 weeks or so... And my people who never do their updates...
So... fortunately, I have AutoIt and my brain (lol)

Basically my approach is the following: I push the new WU and users will receive a notification when they want to do their updates

1. Reboot, install and shutdown at the end of the day
2. schedule a reboot
3. reboot now

As I am in Switzerland I made it in 3 languages: French, German, English (thank to Brett Francis (BrettF))

The thing behind: I download the msu file from the Windows Catalog. I put it on my server where they can download it (internally and externally). I made a dat file (ini file) with the new build of Win 10/11 and the KB file name. I put as well the file time of the exe. Thus it can do an automatique update of my AutoIt prg itself.

My prg will download the dat file compare and do the updates if necessary, then display the gui.

There is a task scheduler that runs my prg.

You 'just' need to run once on the PC and it will deploy by itself. (I push it through a sort of Remote Management software, that run under 'SYSTEM')

What you need to change for you:
- In Write-datFile.au3
Line 17: Your own server (for me it is an UNC path)

$sServerFile = '\\Path\to\myWEB\SERVER\ManageWU.dat'

- In Shutdown.au3:
Line 36: Your own Task Scheduler folder:

Local $iPID = RunWait(@ComSpec & " /c " & 'SCHTASKS /Delete /TN "FOLDER\shutdown" /F', @SystemDir, @SW_HIDE)

- Your Logo !!! (logo.bmp)
- In ManageWU.au3:
Line 12: Your own server (for me it is an UNC path)

#AutoIt3Wrapper_Run_After=copy "%out%" "\\Path\to\myWEB\SERVER\"

Line 66: Your external web adresse:

Global $sRootDwl = 'https://path/to/myWEB/SERVER'

Line 182, 230, 354, 417, 435: Your own Task Scheduler folder:

Local $iPID = RunWait(@ComSpec & ' /c SCHTASKS /Delete /TN "MYFOLDER\Reboot" /F', @SystemDir, @SW_HIDE)
Local $iPID = RunWait(@ComSpec & ' /c SCHTASKS /create /TN "MYFOLDER\ManageWU" /xml "' & $rootdir & '\ScheduleManageWU.xml" /F', @SystemDir, @SW_HIDE, BitOR($STDERR_CHILD, $STDOUT_CHILD))
Local $iPID = RunWait(@ComSpec & ' /c SCHTASKS /create /TN "MYFOLDER\Reboot" /RU "SYSTEM" /xml "' & $rootdir & '\ScheduleTemp.xml" /F', @SystemDir, @SW_HIDE)
Run('SCHTASKS /create /tn "MYFOLDER\Shutdown" /tr "' & $rootdir & '\Shutdown.exe" /sc ONSTART /RU "SYSTEM" /RL "HIGHEST" /F')
Run('SCHTASKS /create /tn "MYFOLDER\Shutdown" /tr "' & $rootdir & '\Shutdown.exe" /sc ONSTART /RU "SYSTEM" /RL "HIGHEST" /F')

FileInstall are in 220-226: just compile other programs and make sure they are in the right place...

FileInstall('logo.bmp', $rootdir & '\', $FC_OVERWRITE + $FC_CREATEPATH)
    FileInstall('.\LngFiles\FRENCH.XML', $rootdir & '\LngFiles\', $FC_OVERWRITE + $FC_CREATEPATH)
    FileInstall('.\LngFiles\ENGLISH.XML', $rootdir & '\LngFiles\', $FC_OVERWRITE + $FC_CREATEPATH)
    FileInstall('.\LngFiles\GERMAN.XML', $rootdir & '\LngFiles\', $FC_OVERWRITE + $FC_CREATEPATH)
    FileInstall('ScheduleManageWU.xml', $rootdir & '\', $FC_OVERWRITE + $FC_CREATEPATH)
    FileInstall('ScheduleReboot.xml', $rootdir & '\', $FC_OVERWRITE + $FC_CREATEPATH)
    FileInstall('Shutdown.exe', $rootdir & '\', $FC_OVERWRITE + $FC_CREATEPATH)

I believe that's all !

Comments are welcome

C.

Edit: typo

WU-pic.png

ManageWU-IE.zip

Edited by cramaboule
Link to post
Share on other sites

The main code:

#RequireAdmin
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseX64=y
#AutoIt3Wrapper_Res_Description=Manage WU
#AutoIt3Wrapper_Res_Fileversion=1.0.0.0
#AutoIt3Wrapper_Res_ProductName=Manage WU
#AutoIt3Wrapper_Res_ProductVersion=1.0.0.0
#AutoIt3Wrapper_Run_Tidy=y
#Tidy_Parameters=/reel
#AutoIt3Wrapper_Run_Au3Stripper=y
#Au3Stripper_Parameters=/mo
#AutoIt3Wrapper_Run_After=copy "%out%" "\\Path\to\myWEB\SERVER\"
#AutoIt3Wrapper_Run_After=start Write-datFile.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs ----------------------------------------------------------------------------

    AutoIt Version: 3.3.16.0
    Author:         Cramaboule
    Destination:    INTERNET sharing
    Date:           July - August 2022

    Script Function: Manage Windows Update and reboot

    V1.0.0.0 : Inital relase
    bug:    Not known
    To do:


#ce ----------------------------------------------------------------------------
#NoTrayIcon
#include-once
#include <FileConstants.au3>
#include <InetConstants.au3>
#include <AutoItConstants.au3>
#include <File.au3>
#include <String.au3>
#include <Array.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <ComboConstants.au3>
#include <MsgBoxConstants.au3>
#include <Date.au3>
#include <WinAPISys.au3>
#include <WinAPIProc.au3>
#include <Misc.au3>
#include 'MultiLang.au3'
;~ ======================================== MUST BE AT 0 WHEN COMPILING FOR REAL =============================
Global $debug = 0
;~ ======================================== MUST BE AT 0 WHEN COMPILING FOR REAL =============================

Global $head = 'Manage Windows Update V1.0.0.0'

If @Compiled Then
    Global $rootdir = 'C:\Users\Public\Documents\ManageWU'
Else
    Global $rootdir = @ScriptDir
EndIf

Global $sFileIni = $rootdir & '\ManageWU.ini'
Global $sFileLog = $rootdir & '\ManageWU.log'
Global $sFileDat = $rootdir & '\ManageWU.dat'
Global $sFileNew = $rootdir & '\new\ManageWU.exe'
Global $sKBFolder = $rootdir & '\KBfiles'
Global $sRootDwl = 'https://path/to/myWEB/SERVER'

_FileWriteLog($sFileLog, 'Starting...')

Sleep(500) ; let time other process to close nicely (during update of itself)

$aProcessList = ProcessList(@ScriptName)
If $aProcessList[0][0] > 1 Then
    _WritingStuff_Exiting('', '2 prg running')
    $sStatus = IniRead($sFileIni, 'Update', 'Status', 'UpToDate')
    If $sStatus = 'RebootAtEndWork' Then ; normaly 1 prg run but in this case we need to kill older process in order to display GUI
        $sFirstPID = IniRead($sFileIni, 'Running', 'PID', 0)
        _WritingStuff_Exiting('', 'Will kill other prg ' & $sFirstPID)
        RunWait(@ComSpec & ' /c TASKKILL /F /PID ' & $sFirstPID, @SystemDir, @SW_HIDE) ;kill older process
    Else
        _WritingStuff_Exiting('', 'An Another process exists, exiting', 1)
    EndIf
EndIf
$sPID = ProcessExists(@ScriptName)
IniWrite($sFileIni, 'Running', 'PID', $sPID)
_WritingStuff_Exiting('', 'PID : ' & $sPID)

If $Cmdline[0] And $Cmdline[1] = '-s' Then
    _WritingStuff_Exiting('', 'command line -s, intalling')
    _Install()
    Run($rootdir & '\' & @ScriptName)
    _WritingStuff_Exiting('', 'Exiting from command line -s', 1)
EndIf

_Update()
If Not (FileExists($rootdir & '\' & @ScriptName)) Then
    _Install(1)
    _FileWriteLog($sFileLog, 'Did the install because $rootdir was not there, exetuting command line -s')
    Run($rootdir & '\' & @ScriptName & ' -s')
    _WritingStuff_Exiting('', 'Exiting from $rootdir not existing', 1)
EndIf

$bIamSYSTEM = 0
$aUser = _WinAPI_GetProcessUser(0)
_FileWriteLog($sFileLog, 'Process is run by : ' & $aUser[0])
If StringInStr($aUser[0], 'syst') Then
    $bIamSYSTEM = 1
    _WritingStuff_Exiting('', 'Process is run by ' & $aUser[0] & ' GUI will be shown at next normal schedule', 1)
EndIf

_CleanFileLog()

$aVersion = _GetVersion()
Switch $aVersion[1]
    Case 'WIN_10'
        $sKBfile = IniRead($sFileDat, 'Windows10', 'KBfile', 'Error')
        $sVersion = IniRead($sFileDat, 'Windows10', 'Version', 'Error')
    Case 'WIN_11'
        $sKBfile = IniRead($sFileDat, 'Windows11', 'KBfile', 'Error')
        $sVersion = IniRead($sFileDat, 'Windows11', 'Version', 'Error')
    Case Else
        _WritingStuff_Exiting('', 'Case Else in Switch $aVersion [1]=' & $aVersion[1], 1)
EndSwitch

If $debug Then
    $sVersion = 1234
    IniWrite($sFileIni, 'Update', 'Status', 'WuInsalled')
    $sStatus = IniRead($sFileIni, 'Update', 'Status', 'UpToDate')
Else
    $sStatus = IniRead($sFileIni, 'Update', 'Status', 'UpToDate')
EndIf
_FileWriteLog($sFileLog, 'KBFile from dat file : ' & $sKBfile)
_FileWriteLog($sFileLog, 'Version from dat file : ' & $sVersion)

If $aVersion[0] <> $sVersion Then
    If $sStatus = 'UpToDate' Then IniWrite($sFileIni, 'Update', 'Status', 'NeedUpdate')
    $sStatus = IniRead($sFileIni, 'Update', 'Status', 'UpToDate')
    _WritingStuff_Exiting('', 'Before Switch', 0)
    Switch $sStatus
        Case 'NeedUpdate'
            _FileWriteLog($sFileLog, 'Downloading : ' & $sRootDwl & $sKBfile)
            DirRemove($sKBFolder, $DIR_REMOVE)
            DirCreate($sKBFolder)
            InetGet($sRootDwl & $sKBfile, $sKBFolder & '\' & $sKBfile, $INET_FORCERELOAD)
            If @error <> 0 Then
                _WritingStuff_Exiting('NeedUpdate', 'Download failed, InetGet got error, Exiting', 1)
            EndIf
            _WritingStuff_Exiting('Installing', 'Download finished, need installing', 0)
            ContinueCase

        Case 'Installing'
            _FileWriteLog($sFileLog, 'Executing : ' & @ComSpec & ' /c wusa.exe "' & $sKBFolder & '\' & $sKBfile & '" /quiet /norestart')
            $ReturnCode = RunWait(@ComSpec & ' /c wusa.exe "' & $sKBFolder & '\' & $sKBfile & '" /quiet /norestart', @SystemDir, @SW_HIDE)
            _FileWriteLog($sFileLog, 'Return code from wusa : ' & $ReturnCode)
            If (($ReturnCode <> '2359301') And ($ReturnCode <> '2359302') And ($ReturnCode <> '3010')) Then
                _WritingStuff_Exiting('', 'Exiting after wusa', 1)
            EndIf
            _WritingStuff_Exiting('WuInsalled', 'wusa has finished, WU Insalled and need reboot', 0)
            If $bIamSYSTEM Then
                _WritingStuff_Exiting('', 'User is SYSTEM prg will exit and GUI will be shown at next schedule', 1)
            Else
                ContinueCase
            EndIf

        Case 'WuInsalled', 'RebootScheduled', 'RebootAtEndWork', 'Rebooting' ; missed somehow reboot and not up to date
            _FileWriteLog($sFileLog, 'Displaying choices')
            _DisplayChoices($aVersion[2])
            _WritingStuff_Exiting('RebootAtEndWork', 'User choose reboot at end work', 0)
            ContinueCase

        Case 'RebootAtEndWork'
            _ShowHidePowerButton(0) ; Hide buttons and show 'update and shut down'
            _WaitToReboot() ; Wait to reboot and block reboot (short time) to write ini
            _WritingStuff_Exiting('', 'User click on "reboot at end work"', 1)

        Case Else
            _WritingStuff_Exiting('', 'Case else $sStatus, Exiting!!!', 1)
    EndSwitch
Else
    If IniRead($sFileIni, 'Update', 'Status', '') = 'RebootScheduled' Then ; Up to date and just rebooted from schedule
        _WritingStuff_Exiting('', 'Removing the scheduled Reboot from the task scheduler')
        Local $iPID = RunWait(@ComSpec & ' /c SCHTASKS /Delete /TN "MYFOLDER\Reboot" /F', @SystemDir, @SW_HIDE)
    EndIf
    _ShowHidePowerButton(1)
    _WritingStuff_Exiting('UpToDate', 'Version of Windows is up to date, Exiting', 1)
EndIf

;~ =============== Functions ====================================================================================================================================

;~ ==============================================================================================================================================================

Func _Update()
    _FileWriteLog($sFileLog, 'Update() Function')
    ;Check for update on the internet
    If Ping('www.google.com') Then
        InetGet($sRootDwl & 'ManageWU.dat', $sFileDat, $INET_FORCERELOAD)
        $FileTimeNew = IniRead($sFileDat, 'ManageWU', 'Version', 'Error')
        $FileTimeOld = FileGetTime($rootdir & '\ManageWU.exe', $FT_MODIFIED, $FT_STRING)
        If $FileTimeNew <> 'Error' And $FileTimeNew > $FileTimeOld Then ; Newer version; connected to the network and have a newer version
            _FileWriteLog($sFileLog, 'New version of ' & @ScriptName)
            DirRemove($rootdir & '\new', $DIR_REMOVE)
            DirCreate($rootdir & '\new')
            InetGet($sRootDwl & 'ManageWU.exe', $sFileNew, $INET_FORCERELOAD)
            Run($sFileNew & ' -s')
            _WritingStuff_Exiting('', 'End of Update() func, Exiting', 1)
        Else
            _FileWriteLog($sFileLog, @ScriptName & ' has not changed')
        EndIf
        _WritingStuff_Exiting('', 'End of Update() func')
    Else
        _WritingStuff_Exiting('', 'PC is not connected to Internet')
    EndIf

EndFunc   ;==>_Update

Func _Install($param = 0)
    DirCreate($rootdir)
    DirCreate($rootdir & '\LngFiles')
    _FileWriteLog($sFileLog, 'Install() Function')
    FileInstall('logo.bmp', $rootdir & '\', $FC_OVERWRITE + $FC_CREATEPATH)
    FileInstall('.\LngFiles\FRENCH.XML', $rootdir & '\LngFiles\', $FC_OVERWRITE + $FC_CREATEPATH)
    FileInstall('.\LngFiles\ENGLISH.XML', $rootdir & '\LngFiles\', $FC_OVERWRITE + $FC_CREATEPATH)
    FileInstall('.\LngFiles\GERMAN.XML', $rootdir & '\LngFiles\', $FC_OVERWRITE + $FC_CREATEPATH)
    FileInstall('ScheduleManageWU.xml', $rootdir & '\', $FC_OVERWRITE + $FC_CREATEPATH)
    FileInstall('ScheduleReboot.xml', $rootdir & '\', $FC_OVERWRITE + $FC_CREATEPATH)
    FileInstall('Shutdown.exe', $rootdir & '\', $FC_OVERWRITE + $FC_CREATEPATH)
    If $param Then IniWrite($sFileIni, 'Update', 'Status', 'UpToDate')
    FileCopy(@ScriptFullPath, $rootdir, $FC_OVERWRITE + $FC_CREATEPATH)
    InetGet($sRootDwl & 'ManageWU.dat', $sFileDat, $INET_FORCERELOAD)
    Local $iPID = RunWait(@ComSpec & ' /c SCHTASKS /create /TN "MYFOLDER\ManageWU" /xml "' & $rootdir & '\ScheduleManageWU.xml" /F', @SystemDir, @SW_HIDE, BitOR($STDERR_CHILD, $STDOUT_CHILD))

EndFunc   ;==>_Install

Func _GetVersion()
    Local $aReturn[0], $sOutput = ''
    _FileWriteLog($sFileLog, '_GetVersion() Function')
    Local $iPID = Run(@ComSpec & ' /c ver', @SystemDir, @SW_HIDE, $STDERR_MERGED)
    While 1
        $sOutput &= StdoutRead($iPID)
        If @error Then  ; Exit the loop if the process closes or StdoutRead returns an error.
            ExitLoop
        EndIf
    WEnd
    $sOutput = _StringBetween($sOutput, 'version ', ']')
    _FileWriteLog($sFileLog, 'Version of PC : ' & $sOutput[0])
    _FileWriteLog($sFileLog, 'Version of Windows : ' & @OSVersion)
    _FileWriteLog($sFileLog, 'Langage of Windows : ' & @MUILang) ;return the current lang, better then @OSLang
    _ArrayAdd($aReturn, $sOutput[0])
    _ArrayAdd($aReturn, @OSVersion)
    _ArrayAdd($aReturn, @MUILang)
    Return $aReturn
EndFunc   ;==>_GetVersion

Func _DisplayChoices($lang)
    _MultiLang_Config(@ScriptDir & '\LngFiles')
    If @error Then
        _WritingStuff_Exiting('', 'Error : cannot set config language files')
    EndIf

    _MultiLang_LoadLangFile($lang)
    If @error Then
        _WritingStuff_Exiting('', 'Error : cannot opening language files')
    EndIf

    $frmFakeGUI = GUICreate('FakeGUI', 0, 0)
    $Form1 = GUICreate($head, 615, 570, -1, -1, $DS_MODALFRAME, $WS_EX_TOPMOST, $frmFakeGUI)
    $Form2 = ''

    $Graphic1 = GUICtrlCreateGraphic(0, -2, 617, 129)
    GUICtrlSetBkColor(-1, 0xFFFFFF)
    $Pic1 = GUICtrlCreatePic('logo.bmp', 16, 30, 64, 64)
    $Label7 = GUICtrlCreateLabel('Windows Update', 113, 39, 286, 46)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetFont(-1, 28, 400, 0, 'Arial')
    GUICtrlSetColor(-1, 0x000000)
    $Label1 = GUICtrlCreateLabel(_MultiLang_GetText('Label1', 1), 55, 138, 500, 325)
    GUICtrlSetFont(-1, 12, 400, 0, 'Arial')
    GUICtrlSetColor(-1, 0x000000)
    $ButtonInstallEndWork = GUICtrlCreateButton(_MultiLang_GetText('ButtonInstallEndWork', 1), 40, 470, 129, 49, BitOR($BS_MULTILINE, $BS_DEFPUSHBUTTON))
    $ButtonSchedule = GUICtrlCreateButton(_MultiLang_GetText('ButtonSchedule', 1), 244, 470, 129, 49, $BS_MULTILINE)
    $ButtonReboot = GUICtrlCreateButton(_MultiLang_GetText('ButtonReboot', 1), 448, 470, 129, 49, $BS_MULTILINE)
    $Label18 = GUICtrlCreateLabel('Your IT Team !', 505, 522, 100, 20)
    GUICtrlSetFont(-1, 8, 400, 0, "Arial")
    GUICtrlSetColor(-1, 0x000000)
    GUISetState(@SW_SHOW)
    WinSetOnTop($Form1, '', $WINDOWS_ONTOP)
    WinWaitActive($Form1, '', 10)
    _FileWriteLog($sFileLog, 'Show GUI')

    While 1
        $nMsg1 = GUIGetMsg($Form1)
        Switch $nMsg1
            Case $GUI_EVENT_CLOSE
                _WritingStuff_Exiting('Rebooting', '$GUI_EVENT_CLOSE 1, Rebooting')
                Sleep(100)
                Shutdown($SD_REBOOT + $SD_FORCE)

            Case $ButtonReboot
                _WritingStuff_Exiting('Rebooting', 'User choose reboot', 0)
                Sleep(100)
                Shutdown($SD_REBOOT + $SD_FORCE)

            Case $ButtonSchedule
                $Form2 = GUICreate(_MultiLang_GetText('Form2'), 170, 180, -1, -1, $DS_MODALFRAME, $WS_EX_TOPMOST, $frmFakeGUI)
                $Label2 = GUICtrlCreateLabel(_MultiLang_GetText('Label2', 1), 25, 10, 120, 40)
                GUICtrlSetFont(-1, 10, 400, 0, 'Arial')
                $Combo1 = GUICtrlCreateCombo('00', 25, 55, 45, 25)
                GUICtrlSetData($Combo1, '01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23', '02')
                $Combo2 = GUICtrlCreateCombo('00', 94, 55, 45, 25)
                GUICtrlSetData($Combo2, '01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59', '00')
                $Label5 = GUICtrlCreateLabel(':', 80, 55, 10, 20)
                GUICtrlSetFont(-1, 10, 800, 0, 'Arial')
                $ButtonOK = GUICtrlCreateButton(_MultiLang_GetText('ButtonOK'), 10, 100, 65, 30, $BS_DEFPUSHBUTTON)
                $ButtonCancel = GUICtrlCreateButton(_MultiLang_GetText('ButtonCancel'), 90, 100, 65, 30)
                GUISetState(@SW_SHOW)
                ControlSend($Form2, '', $Combo1, '{down}')

                While 1
                    $nMsg2 = GUIGetMsg($Form2)
                    Switch $nMsg2
                        Case $GUI_EVENT_CLOSE
                            _WritingStuff_Exiting('Rebooting', '$GUI_EVENT_CLOSE 2, Rebooting', 0)
                            Sleep(100)
                            Shutdown($SD_REBOOT + $SD_FORCE)
                        Case $ButtonOK
                            GUISetState(@SW_HIDE, $Form2)
                            ; calcul of time/date: if time < now then schedule for tommorow
                            $hours = GUICtrlRead($Combo1)
                            If $hours > 23 Then $hours = 0
                            $minutes = GUICtrlRead($Combo2)
                            If $minutes >= 60 Then
                                $minutes = 1
                            EndIf
                            $time = $hours & ':' & $minutes
                            $diffmin = _DateDiff('n', _NowCalc(), _NowCalcDate() & ' ' & $time) ; minutes
                            If ($diffmin >= -2 And $diffmin <= 2) Then
                                $date = StringTrimRight(_DateAdd('n', 3, _NowCalc()), 3)
                                $aDate = StringSplit($date, ' ')
                                $time = $aDate[2]
                            EndIf
                            $diff = _DateDiff('D', _NowCalc(), _NowCalcDate() & ' ' & $time)
                            If $diff <> 0 Then $diff = 1
                            $finaldate = _DateAdd('D', $diff, _NowCalcDate())
                            $sStartDateTime = $finaldate & ' ' & $time & ':00'
                            $sStartDateTime = StringReplace($sStartDateTime, '/', '-')
                            $sStartDateTime = StringReplace($sStartDateTime, ' ', 'T')
                            Local $hFile = FileOpen($rootdir & '\ScheduleReboot.xml', $FO_READ + $FO_UTF16_LE)
                            Local $sFileContent = FileRead($hFile)
                            FileClose($hFile)
                            $sFileContent = StringReplace($sFileContent, '***DateTime***', $sStartDateTime)
                            Local $hFile = FileOpen($rootdir & '\ScheduleTemp.xml', $FO_OVERWRITE + $FO_UTF16_LE)
                            FileWrite($hFile, $sFileContent)
                            FileClose($hFile)
                            Local $iPID = RunWait(@ComSpec & ' /c SCHTASKS /create /TN "MYFOLDER\Reboot" /RU "SYSTEM" /xml "' & $rootdir & '\ScheduleTemp.xml" /F', @SystemDir, @SW_HIDE)
                            MsgBox($MB_ICONINFORMATION, _MultiLang_GetText('MsgboxTitle'), _MultiLang_GetText('MsgboxTxt') & ' ' & _DateTimeFormat($finaldate & ' ' & $time, 0), 15, $frmFakeGUI)
                            FileDelete($rootdir & '\ScheduleTemp.xml')
                            _WritingStuff_Exiting('RebootScheduled', 'User choose reboot scheduled : ' & _DateTimeFormat($finaldate & ' ' & $time, 0), 1)

                        Case $ButtonCancel
                            GUISetState(@SW_SHOW, $Form1)
                            GUIDelete($Form2)
                            _FileWriteLog($sFileLog, 'User click on Cancel')
                            ExitLoop

                    EndSwitch
                WEnd

            Case $ButtonInstallEndWork
                GUIDelete($Form1)
                GUIDelete($Form2)
                ExitLoop

        EndSwitch
    WEnd
EndFunc   ;==>_DisplayChoices

Func _ShowHidePowerButton($bShowHide) ; 1 to show, 0 to hide
    $bShowHide = Not ($bShowHide) ; 0 to show, 1 to hide
    RegWrite('HKLM\SOFTWARE\Microsoft\PolicyManager\default\Start\HideSignOut', 'value', 'REG_DWORD', Int($bShowHide))
    RegWrite('HKLM\SOFTWARE\Microsoft\PolicyManager\default\Start\HideSleep', 'value', 'REG_DWORD', Int($bShowHide))
    RegWrite('HKLM\SOFTWARE\Microsoft\PolicyManager\default\Start\HideSwitchAccount', 'value', 'REG_DWORD', Int($bShowHide))
    RegWrite('HKLM\SOFTWARE\Microsoft\PolicyManager\default\Start\HideLock', 'value', 'REG_DWORD', Int($bShowHide))
    RegWrite('HKLM\SOFTWARE\Microsoft\PolicyManager\default\Start\HideRestart', 'value', 'REG_DWORD', Int($bShowHide))
    RegWrite('HKLM\SOFTWARE\Microsoft\PolicyManager\current\device\Start', 'HideSignOut', 'REG_DWORD', Int($bShowHide))
    RegWrite('HKLM\SOFTWARE\Microsoft\PolicyManager\current\device\Start', 'HideSleep', 'REG_DWORD', Int($bShowHide))
    RegWrite('HKLM\SOFTWARE\Microsoft\PolicyManager\current\device\Start', 'HideSwitchAccount', 'REG_DWORD', Int($bShowHide))
    RegWrite('HKLM\SOFTWARE\Microsoft\PolicyManager\current\device\Start', 'HideLock', 'REG_DWORD', Int($bShowHide))
    RegWrite('HKLM\SOFTWARE\Microsoft\PolicyManager\current\device\Start', 'HideRestart', 'REG_DWORD', Int($bShowHide))
    RegWrite('HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System', 'shutdownwithoutlogon', 'REG_DWORD', Not (Int($bShowHide))) ; 1 to show, 0 to hide
    If $bShowHide Then
        RegWrite('HKLM\SOFTWARE\Microsoft\WindowsUpdate\Orchestrator', 'ShutdownFlyoutOptions', 'REG_DWORD', 10) ; will display Install and shut down
        RegWrite('HKLM\SOFTWARE\Microsoft\WindowsUpdate\Orchestrator', 'EnhancedShutdownEnabled', 'REG_DWORD', Int($bShowHide))
    Else
        RegWrite('HKLM\SOFTWARE\Microsoft\WindowsUpdate\Orchestrator', 'ShutdownFlyoutOptions', 'REG_DWORD', Int($bShowHide))
        RegDelete('HKLM\SOFTWARE\Microsoft\WindowsUpdate\Orchestrator', 'EnhancedShutdownEnabled')
    EndIf
EndFunc   ;==>_ShowHidePowerButton

Func _WaitToReboot()
    _WritingStuff_Exiting('', 'In Func _WaitToReboot()', 0)
    Global $frmFakeGUI = GUICreate('FakeGUI', 0, 0)
    Global $g_hForm = GUICreate('Wait to reboot', 0, 0, 0, 0, $DS_MODALFRAME, -1, $frmFakeGUI)
    GUIRegisterMsg($WM_QUERYENDSESSION, 'WM_QUERYENDSESSION')
    WinSetTrans($g_hForm, 'Wait to reboot', 0)
    GUISetState(@SW_SHOW)

    ; Set the highest shutdown priority for the current process to prevent closure the other processes.
    _WinAPI_SetProcessShutdownParameters(0x03FF)
    _WinAPI_ShutdownBlockReasonCreate($g_hForm, 'This application is blocking system shutdown because of WU.')

    While 1
        $nMsg3 = GUIGetMsg($g_hForm)
        Switch $nMsg3
            Case $GUI_EVENT_CLOSE
                _WritingStuff_Exiting('', '$GUI_EVENT_CLOSE 3', 0)
                _ShowHidePowerButton(1)
                Run('SCHTASKS /create /tn "MYFOLDER\Shutdown" /tr "' & $rootdir & '\Shutdown.exe" /sc ONSTART /RU "SYSTEM" /RL "HIGHEST" /F')
                _WritingStuff_Exiting('UpToDate', '', 0)
                _WinAPI_ShutdownBlockReasonDestroy($g_hForm)
                Shutdown($SD_REBOOT + $SD_FORCE)
        EndSwitch
        _ShowHidePowerButton(0)
        Sleep(200)
    WEnd

EndFunc   ;==>_WaitToReboot

Func WM_QUERYENDSESSION($hWnd, $iMsg, $wParam, $lParam)
    #forceref $iMsg, $wParam, $lParam
    _WritingStuff_Exiting('', 'In Func WM_QUERYENDSESSION', 0)
    Switch $hWnd
        Case $g_hForm
            If _WinAPI_ShutdownBlockReasonQuery($g_hForm) Then
                _ShowHidePowerButton(1)
                Run('SCHTASKS /create /tn "MYFOLDER\Shutdown" /tr "' & $rootdir & '\Shutdown.exe" /sc ONSTART /RU "SYSTEM" /RL "HIGHEST" /F')
                _WritingStuff_Exiting('UpToDate', '', 0)
                _WinAPI_ShutdownBlockReasonDestroy($g_hForm)
                Shutdown($SD_REBOOT + $SD_FORCE)
                Return 0
            EndIf
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_QUERYENDSESSION

Func _CleanFileLog()
    $IniDate = IniRead($sFileIni, 'LogFile', 'DateOfDeletion', '')
;~  Clean log file if older than 30 days
    If (($IniDate = '') Or (_DateAdd('D', -30, _NowCalcDate()) >= $IniDate)) Then
        FileDelete($sFileLog)
        IniWrite($sFileIni, 'LogFile', 'DateOfDeletion', _NowCalcDate())
        _WritingStuff_Exiting('', 'Log file cleared')

    Else
        _WritingStuff_Exiting('', 'No need to clean log file')
    EndIf
EndFunc   ;==>_CleanFileLog

Func _WritingStuff_Exiting($ssStatus = '', $ssOtherMessage = '', $eexit = 0)
    If $ssStatus <> '' Then IniWrite($sFileIni, 'Update', 'Status', $ssStatus)

    If $ssOtherMessage <> '' Then _FileWriteLog($sFileLog, $ssOtherMessage)

    _FileWriteLog($sFileLog, '$ssStatus : ' & IniRead($sFileIni, 'Update', 'Status', 'Nothing'))

    If $eexit Then
        _FileWriteLog($sFileLog, 'Bye...')
        Exit
    EndIf
EndFunc   ;==>_WritingStuff_Exiting

 

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By janrenzlow
      Hello,
      at my work I have numerous notebooks to install. Among other things, I want to search and install Windows updates all automatically. This succeeds quite well with the one-liner:
      Runwait (@ComSpec & ' /c ' & 'explorer.exe ms-settings:windowsupdate-action','')
      But how can I catch the event when all updates are installed and then restart the computer first?
      Thanks for your answers in advance
      Jan
    • By DesireDenied
      Hey guys,
      I having some hard times getting false-positive, probably because I am trying to execute my AutoUpdater.
      Here is my code:
       
      Global $iUpdateTimer = 0 While 1 checkUpdates(10) WEnd Func checkUpdates($iDelay = 10) $iDelay = $iDelay * 1000 * 60 If TimerDiff($iUpdateTimer) > $iDelay Then ConsoleWrite('checking for updates...' & @CRLF) $iUpdateTimer = TimerInit() If FileExists('AutoUpdater.exe') Then ShellExecuteWait('AutoUpdater.exe') ; this is the line which cause my problem EndIf EndFunc And AutoUpdater code:
      #include <MsgBoxConstants.au3> #include <FileConstants.au3> Global $sExecName = 'test.exe' Global $sUpdatePath = @UserProfileDir &'\desktop\AnyAppName\update\'& $sExecName Global $sUserPath = @UserProfileDir &'\desktop\AnyAppName\'& $sExecName Global $sCopyright = 'someUniqueStringHere' If Not FileExists($sUpdatePath) Then Exit 0 If FileGetVersion($sUpdatePath, $FV_LEGALCOPYRIGHT) <> $sCopyright Then Exit 0 ; checking if we really want to update and execute the file If FileGetVersion($sUpdatePath) > FileGetVersion($sUserPath) Then $iResponse = MsgBox(BitOR($MB_YESNO, $MB_ICONQUESTION),'AnyAppName', 'There is an update available, would you like to update?') If $iResponse == $IDYES Then If ProcessExists($sExecName) Then ProcessClose($sExecName) Sleep(500) EndIf FileCopy($sUpdatePath, $sUserPath, $FC_OVERWRITE) Sleep(3000) ShellExecute($sUserPath) Exit 1 EndIf EndIf Exit 0 I am not trying to ask, why is my code is getting recognized as false-positive, because this is quite obvious, but is there any other way to get things done without running external process?
       

    • By Simpel
      Hi, I created a gui with date field but formatted as time in HH:mm. It always shows "now-time". Even if I try to set it with GUICtrlSetData.
      #include <DateTimeConstants.au3> #include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> Example() Func Example() GUICreate("My GUI get date", 200, 200, 800, 200) Local $idDate = GUICtrlCreateDate("", 20, 20, 100, 20, $DTS_TIMEFORMAT) ; to select a specific default format Local $sStyle = "HH:mm" ; Just display hours and minutes <<<<<<<<<<<<<<<<<<<<<<<<<<<< GUICtrlSendMsg($idDate, $DTM_SETFORMATW, 0, $sStyle) ConsoleWrite(GUICtrlRead($idDate) & @CRLF) GUICtrlSetData($idDate, "00:00") ConsoleWrite(GUICtrlRead($idDate) & @CRLF) GUISetState(@SW_SHOW) ; Loop until the user exits. While GUIGetMsg() <> $GUI_EVENT_CLOSE WEnd MsgBox($MB_SYSTEMMODAL, "Time", GUICtrlRead($idDate)) EndFunc ;==>Example How can I set the time field with another time and how can this field be set blank?
      Regards, Conrad
    • By Vijaya7890
      Hi...
      I am trying to add a record in Binary Table in an MSI file.
      As Binary table has two fields/columns Name &Data , I am giving any string for Name(column 1) and streaming binary data from a VBS file into Data(column 2) .
      I have tried with VB script and it is working fine,
      Here is the VB Script file
      Dim Installer Dim Database Dim View Dim Record Dim query query="INSERT INTO `Binary` (`Name`, `Data`) VALUES ('NewBlob', ?)" Set Installer = CreateObject("WindowsInstaller.Installer") Set Record = Installer.CreateRecord(1) Record.SetStream 1, "C:\Users\Admin\Desktop\coder\CA_Test.vbs" Set Database = Installer.OpenDatabase("C:\Users\Admin\Desktop\7z920.msi", 1) Set View = Database.OpenView(query) View.Execute Record Database.Commit when i tried the same thing in autoit it is not working and it is not giving any syntax error.
      AutoIt file: 
      #include <File.au3> $idVarInput_CAName="CA_Test" $filepath="C:\Users\Admin\Desktop\coder\CA_Test.vbs" $idVarInput_MSIPath="C:\Users\Admin\Desktop\7z920.msi" $value="?" Local $Query = "INSERT INTO Binary(Name,Data) VALUES('" & $idVarInput_CAName & "','" & $value & "')" $oInstaller = ObjCreate("WindowsInstaller.Installer") $oRec=$oInstaller.CreateRecord(1) $r=$oRec.SetStream(1,$filepath) $oDB = $oInstaller.OpenDataBase($idVarInput_MSIPath,1) $oView = $oDB.OpenView($Query) $oView.Execute($oRec) $oDB.commit() Please Help!
      Thanks...
    • By Nnet
      Hello, Im using GUICtrlCreateListView to make table with items. But when it is new item with same first column a want just update allready existing row. Its posible?

      I cant figure it, if its better use other funkcion pls. tell me.
      Thank
×
×
  • Create New...