When deploying software via SCCM I thought wouldn’t it be nice if there was greater flexibility regarding system reboot prompts for the end user. Sure you can enable a maintenance window and push your software out during that time, but we have at times been caught where a software push is needed during business hours.
So I came up with this PowerShell script which you can run as part of a task sequence when deploying emergency/unscheduled software installs. The script generates a GUI which provides the end-user with three options;
- Restart the computer
- Schedule a restart (note in here I have hard-coded this for 6pm)
- Cancel the restart
The script also starts a count-down timer to automatically restart the computer after 3 minutes if no user interaction occurs.
Example Script Use – SCCM TS
In the below example we are going to create a Package in SCCM which contains the script file, you will also need to include two exe files from MDT which allow you to run the script in interactive mode.
Locate ServiceUI.exe and TSProgressUI.exe (obviously picking the x86 or x64 where applicable) and add these into your package source. You should have something that looks like this ;
Now add a Run Command Line entry into your TS and use the following command line;
ServiceUI.exe -process:TSProgressUI.exe %SYSTEMROOT%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -WindowStyle Hidden -ExecutionPolicy Bypass -File CustomRestart.ps1
When the Task Sequence is run, you should now have the restart prompt appear;
Script Source
<# .NOTES -------------------------------------------------------------------------------- Code generated by: SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.128 Generated on: 04/10/2016 10:13 Generated by: Maurice.Daly -------------------------------------------------------------------------------- .DESCRIPTION Provides an reboot prompt which counts down from 3 minutes and allows the end user to schedule or cancel the reboot. #> #---------------------------------------------- #region Import Assemblies #---------------------------------------------- [void][Reflection.Assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089') [void][Reflection.Assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089') [void][Reflection.Assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a') #endregion Import Assemblies #Define a Param block to use custom parameters in the project #Param ($CustomParameter) function Main { Param ([String]$Commandline) if((Call-MainForm_psf) -eq 'OK') { } $global:ExitCode = 0 #Set the exit code for the Packager } #endregion Source: Startup.pss #region Source: MainForm.psf function Call-MainForm_psf { #---------------------------------------------- #region Import the Assemblies #---------------------------------------------- [void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089') [void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089') [void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a') #endregion Import Assemblies #---------------------------------------------- #region Generated Form Objects #---------------------------------------------- [System.Windows.Forms.Application]::EnableVisualStyles() $MainForm = New-Object 'System.Windows.Forms.Form' $panel2 = New-Object 'System.Windows.Forms.Panel' $ButtonCancel = New-Object 'System.Windows.Forms.Button' $ButtonSchedule = New-Object 'System.Windows.Forms.Button' $ButtonRestartNow = New-Object 'System.Windows.Forms.Button' $panel1 = New-Object 'System.Windows.Forms.Panel' $labelITSystemsMaintenance = New-Object 'System.Windows.Forms.Label' $labelSecondsLeftToRestart = New-Object 'System.Windows.Forms.Label' $labelTime = New-Object 'System.Windows.Forms.Label' $labelInOrderToApplySecuri = New-Object 'System.Windows.Forms.Label' $timerUpdate = New-Object 'System.Windows.Forms.Timer' $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState' #endregion Generated Form Objects #---------------------------------------------- # User Generated Script #---------------------------------------------- $TotalTime = 180 #in seconds $MainForm_Load={ #TODO: Initialize Form Controls here $labelTime.Text = "{0:D2}" -f $TotalTime #$TotalTime #Add TotalTime to current time $script:StartTime = (Get-Date).AddSeconds($TotalTime) #Start the timer $timerUpdate.Start() } $timerUpdate_Tick={ # Define countdown timer [TimeSpan]$span = $script:StartTime - (Get-Date) #Update the display $labelTime.Text = "{0:N0}" -f $span.TotalSeconds $timerUpdate.Start() if ($span.TotalSeconds -le 0) { $timerUpdate.Stop() Restart-Computer -Force } } $ButtonRestartNow_Click = { # Restart the computer immediately Restart-Computer -Force } $ButtonSchedule_Click={ # Schedule restart for 6pm (schtasks /create /sc once /tn "Post Maintenance Restart" /tr "shutdown - r -f ""restart""" /st 18:00 /f) $MainForm.Close() } $ButtonCancel_Click={ #TODO: Place custom script here $MainForm.Close() } $labelITSystemsMaintenance_Click={ #TODO: Place custom script here } $panel2_Paint=[System.Windows.Forms.PaintEventHandler]{ #Event Argument: $_ = [System.Windows.Forms.PaintEventArgs] #TODO: Place custom script here } $labelTime_Click={ #TODO: Place custom script here } # --End User Generated Script-- #---------------------------------------------- #region Generated Events #---------------------------------------------- $Form_StateCorrection_Load= { #Correct the initial state of the form to prevent the .Net maximized form issue $MainForm.WindowState = $InitialFormWindowState } $Form_StoreValues_Closing= { #Store the control values } $Form_Cleanup_FormClosed= { #Remove all event handlers from the controls try { $ButtonCancel.remove_Click($buttonCancel_Click) $ButtonSchedule.remove_Click($ButtonSchedule_Click) $ButtonRestartNow.remove_Click($ButtonRestartNow_Click) $panel2.remove_Paint($panel2_Paint) $labelITSystemsMaintenance.remove_Click($labelITSystemsMaintenance_Click) $labelTime.remove_Click($labelTime_Click) $MainForm.remove_Load($MainForm_Load) $timerUpdate.remove_Tick($timerUpdate_Tick) $MainForm.remove_Load($Form_StateCorrection_Load) $MainForm.remove_Closing($Form_StoreValues_Closing) $MainForm.remove_FormClosed($Form_Cleanup_FormClosed) } catch [Exception] { } } #endregion Generated Events #---------------------------------------------- #region Generated Form Code #---------------------------------------------- $MainForm.SuspendLayout() $panel2.SuspendLayout() $panel1.SuspendLayout() # # MainForm # $MainForm.Controls.Add($panel2) $MainForm.Controls.Add($panel1) $MainForm.Controls.Add($labelSecondsLeftToRestart) $MainForm.Controls.Add($labelTime) $MainForm.Controls.Add($labelInOrderToApplySecuri) $MainForm.AutoScaleDimensions = '6, 13' $MainForm.AutoScaleMode = 'Font' $MainForm.BackColor = 'White' $MainForm.ClientSize = '373, 279' $MainForm.MaximizeBox = $False $MainForm.MinimizeBox = $False $MainForm.Name = 'MainForm' $MainForm.ShowIcon = $False $MainForm.ShowInTaskbar = $False $MainForm.StartPosition = 'CenterScreen' $MainForm.Text = 'Systems Maintenance' $MainForm.TopMost = $True $MainForm.add_Load($MainForm_Load) # # panel2 # $panel2.Controls.Add($ButtonCancel) $panel2.Controls.Add($ButtonSchedule) $panel2.Controls.Add($ButtonRestartNow) $panel2.BackColor = 'ScrollBar' $panel2.Location = '0, 205' $panel2.Name = 'panel2' $panel2.Size = '378, 80' $panel2.TabIndex = 9 $panel2.add_Paint($panel2_Paint) # # ButtonCancel # $ButtonCancel.Location = '250, 17' $ButtonCancel.Name = 'ButtonCancel' $ButtonCancel.Size = '77, 45' $ButtonCancel.TabIndex = 7 $ButtonCancel.Text = 'Cancel' $ButtonCancel.UseVisualStyleBackColor = $True $ButtonCancel.add_Click($buttonCancel_Click) # # ButtonSchedule # $ButtonSchedule.Font = 'Microsoft Sans Serif, 8.25pt, style=Bold' $ButtonSchedule.Location = '139, 17' $ButtonSchedule.Name = 'ButtonSchedule' $ButtonSchedule.Size = '105, 45' $ButtonSchedule.TabIndex = 6 $ButtonSchedule.Text = 'Schedule - 6pm' $ButtonSchedule.UseVisualStyleBackColor = $True $ButtonSchedule.add_Click($ButtonSchedule_Click) # # ButtonRestartNow # $ButtonRestartNow.Font = 'Microsoft Sans Serif, 8.25pt, style=Bold' $ButtonRestartNow.ForeColor = 'DarkRed' $ButtonRestartNow.Location = '42, 17' $ButtonRestartNow.Name = 'ButtonRestartNow' $ButtonRestartNow.Size = '91, 45' $ButtonRestartNow.TabIndex = 0 $ButtonRestartNow.Text = 'Restart Now' $ButtonRestartNow.UseVisualStyleBackColor = $True $ButtonRestartNow.add_Click($ButtonRestartNow_Click) # # panel1 # $panel1.Controls.Add($labelITSystemsMaintenance) $panel1.BackColor = '0, 114, 198' $panel1.Location = '0, 0' $panel1.Name = 'panel1' $panel1.Size = '375, 67' $panel1.TabIndex = 8 # # labelITSystemsMaintenance # $labelITSystemsMaintenance.Font = 'Microsoft Sans Serif, 14.25pt' $labelITSystemsMaintenance.ForeColor = 'White' $labelITSystemsMaintenance.Location = '11, 18' $labelITSystemsMaintenance.Name = 'labelITSystemsMaintenance' $labelITSystemsMaintenance.Size = '269, 23' $labelITSystemsMaintenance.TabIndex = 1 $labelITSystemsMaintenance.Text = 'IT Systems Maintenance' $labelITSystemsMaintenance.TextAlign = 'MiddleLeft' $labelITSystemsMaintenance.add_Click($labelITSystemsMaintenance_Click) # # labelSecondsLeftToRestart # $labelSecondsLeftToRestart.AutoSize = $True $labelSecondsLeftToRestart.Font = 'Microsoft Sans Serif, 9pt, style=Bold' $labelSecondsLeftToRestart.Location = '87, 176' $labelSecondsLeftToRestart.Name = 'labelSecondsLeftToRestart' $labelSecondsLeftToRestart.Size = '155, 15' $labelSecondsLeftToRestart.TabIndex = 5 $labelSecondsLeftToRestart.Text = 'Seconds left to restart :' # # labelTime # $labelTime.AutoSize = $True $labelTime.Font = 'Microsoft Sans Serif, 9pt, style=Bold' $labelTime.ForeColor = '192, 0, 0' $labelTime.Location = '237, 176' $labelTime.Name = 'labelTime' $labelTime.Size = '43, 15' $labelTime.TabIndex = 3 $labelTime.Text = '00:60' $labelTime.TextAlign = 'MiddleCenter' $labelTime.add_Click($labelTime_Click) # # labelInOrderToApplySecuri # $labelInOrderToApplySecuri.Font = 'Microsoft Sans Serif, 9pt' $labelInOrderToApplySecuri.Location = '12, 84' $labelInOrderToApplySecuri.Name = 'labelInOrderToApplySecuri' $labelInOrderToApplySecuri.Size = '350, 83' $labelInOrderToApplySecuri.TabIndex = 2 $labelInOrderToApplySecuri.Text = 'In order to apply security patches and updates for your system, your machine must be restarted. If you do not wish to restart you computer at this time please click on the cancel button below.' # # timerUpdate # $timerUpdate.add_Tick($timerUpdate_Tick) $panel1.ResumeLayout() $panel2.ResumeLayout() $MainForm.ResumeLayout() #endregion Generated Form Code #---------------------------------------------- #Save the initial state of the form $InitialFormWindowState = $MainForm.WindowState #Init the OnLoad event to correct the initial state of the form $MainForm.add_Load($Form_StateCorrection_Load) #Clean up the control events $MainForm.add_FormClosed($Form_Cleanup_FormClosed) #Store the control values when form is closing $MainForm.add_Closing($Form_StoreValues_Closing) #Show the Form return $MainForm.ShowDialog() } #endregion Source: MainForm.psf #Start the application Main ($CommandLine)
Download Link
The script is available to download from:
https://gallery.technet.microsoft.com/scriptcenter/Custom-PowerShell-GUI-7c7fbda8
Great Script. does just what we need, but will remove the Cancel option as our users would click that every time. 🙂
How do I change it so it shows minutes and seconds?
Ran into issue with leveraging TSProgressUI.exe for the process filter. Weird thing, I look at running processes and it was right there infront of me
API [ProcessIdToSessionId] Error: [5]
Process Not Found: [tsprogressui.exe]
I resorted to leveraging explorer.exe and worked as exepected
What’s the benefit of doing this instead of just doing a normal application deployment with “ConfigMgr requires a mandatory reboot” set and Allow Installations and Reboots during Maintenance windows checked?
Hi John,
The benefit of doing this is if you have to urgently push software to your users but you want them to have some control over when a mandatory reboot occurs. In my own environment I have had several instances where I have been advised to push out an application update that requires a reboot, for some users the software was critical and others not so much so providing them with an option to reschedule keeps the user population content. This feature is actually due to be built into future updates of the product.
Maurice
So, what’s the minimum version of Powershell necessary for this to run? And yes, there’s an actual reason I need to ask this question.
Hi Bruce,
I have tested the code back as far as PowerShell 2.0 with no issues. I hope that helps.
Maurice