Have you ever needed to prompt a user for a reboot at the end of a setup process? Here is how you do it with a form, which includes a remind me.
Code.ps1
# MrNetTek
# eddiejackson.net
# 12/19/2023
# free for public use
# free to claim as your own
# LOAD MAIN FUNCTION
function Start-Reboot {
$global:counter = 1
$ErrorActionPreference = 'silentlycontinue'
# play alert sound
$sound = new-Object System.Media.SoundPlayer;
$sound.SoundLocation="c:\WINDOWS\Media\notify.wav";
$sound.PlayLooping();
$flag=$false;
1..10 | foreach {
if($_ -gt 1){$flag=$true} else{sleep -s 1}
if($flag) {$sound.Stop() }
}
# LOAD ASSEMBLIES
Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName PresentationFramework
[System.Windows.Forms.Application]::EnableVisualStyles()
# INSTANTIATE OBJECTS
$form = New-Object 'System.Windows.Forms.Form'
$label1 = New-Object System.Windows.Forms.Label
$label2 = New-Object System.Windows.Forms.Label
$label3 = New-Object System.Windows.Forms.Label
$button1 = New-Object 'System.Windows.Forms.Button'
$button2 = New-Object 'System.Windows.Forms.Button'
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
$Form_StateCorrection_Load=
{
$form.WindowState = $InitialFormWindowState
}
# MAIN FORM
# $form.Icon = [Drawing.Icon]::ExtractAssociatedIcon((Get-Command powershell).Path)
$form.Controls.Add($button1)
$form.Controls.Add($button2)
$form.Controls.Add($label1)
$form.Controls.Add($label2)
$form.Controls.Add($label3)
$form.AutoScaleDimensions = '8, 17'
$form.AutoScaleMode = 'Font'
$form.ClientSize = '616, 262'
$form.FormBorderStyle = 'FixedDialog'
$form.Margin = '5, 5, 5, 5'
$form.MaximizeBox = $False
$form.MinimizeBox = $False
$form.ControlBox = $False
$form.Name = 'EMPTYREBOOT'
$form.StartPosition = 'CenterScreen'
$form.Text = 'Empty Reboot Test'
$form.ShowInTaskbar = $False
$form.TopMost = $True # force window to stay on top
$form.add_Load($form_Load)
# LABEL1
$label1.Location = New-Object System.Drawing.Point(240,20)
$label1.Font = 'Calibri, 13.25pt'
$label1.Size = New-Object System.Drawing.Size(550,50)
$label1.Text = 'Empty Reboot Test'
# LABEL2
$label2.Location = New-Object System.Drawing.Point(100,80)
$label2.Font = 'Calibri, 11.25pt'
$label2.Size = New-Object System.Drawing.Size(440,50)
$label2.Text = 'Please save and close all your work. Click Restart to complete setup.'
# LABEL3
$label3.Location = New-Object System.Drawing.Point(275,230)
$label3.Font = 'Calibri, 9.25pt'
$label3.Size = New-Object System.Drawing.Size(120,80)
$label3.Text = 'MRNETTEK 2023'
# button
$button1.Font = 'Calibri, 12.25pt'
$button1.DialogResult = 'OK'
$button1.Location = '90, 155'
$button1.Margin = '5, 5, 5, 5'
$button1.Name = 'buttonOK1'
$button1.Size = '200, 50'
$button1.BackColor ="LightGray"
$button1.ForeColor ="black"
$button1.Text = '&Restart'
$button1.UseCompatibleTextRendering = $True
$button1.UseVisualStyleBackColor = $False
$button1.Add_Click({$form.Add_FormClosing({$_.Cancel=$false});Restart-Computer -Force})
$button1.TabIndex = 1
# button
$button2.Font = 'Calibri, 12.25pt'
$button2.DialogResult = 'OK'
$button2.Location = '330, 155'
$button2.Margin = '5, 5, 5, 5'
$button2.Name = 'buttonOK2'
$button2.Size = '200, 50'
$button2.BackColor ="LightGray"
$button2.ForeColor ="black"
$button2.Text = '&Remind Me'
$button2.UseCompatibleTextRendering = $True
$button2.UseVisualStyleBackColor = $False
$button2.Add_Click($button2_click)
$button2.TabIndex = 2
# FORM CONFIG
$InitialFormWindowState = $form.WindowState
$form.add_Load($Form_StateCorrection_Load)
# FOCUS THIS PROCESS
$WindowState = '[DllImport("user32.dll")] public static extern bool ShowWindow(int handle, int state);'
add-type -name win -member $WindowState -namespace native
[native.win]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle, 0)
SetForegroundWindow(this.Handle)
# SHOW FORM
$form.add_FormClosing({$_.Cancel=$true})
[void] $form.ShowDialog()
$counter
}
$button2_click ={
if ($counter -lt 3) {
$script:counter++
$form.WindowState = [System.Windows.Forms.FormWindowState]::Minimized
$form.Hide()
#Start-Sleep -Seconds 30 # for testing purposes only
Start-Sleep -Seconds 3600 #1 hour
CheckCounter
$form.Show()
$form.WindowState = [System.Windows.Forms.FormWindowState]::Normal
}
}
function CloseForm {
$form.Close()
}
# RUN MAIN FUNCTION
$global:counter = 1
Start-Reboot