Skip to content

Instantly share code, notes, and snippets.

@pamanes
Created January 6, 2024 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pamanes/2124e5e3797c8444308a40be407d5bc2 to your computer and use it in GitHub Desktop.
Save pamanes/2124e5e3797c8444308a40be407d5bc2 to your computer and use it in GitHub Desktop.
This PowerShell script runs with elevated privileges if not already, then executes a script and restarts the machine and continues after logon, a PowerShell window will appear after logon where the script runs
<#
.SYNOPSIS
This PowerShell script runs with elevated privileges if not already, then executes a script and restarts
the machine and continues after logon, a PowerShell window will appear after logon where the script will resume
.NOTES
File Name : restart_and_continue_script.ps1
Author : Alejandro Palacios (pamanes@gmail.com)
Prerequisite : PowerShell V3+,
Minimum Execution policy: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
run in powershell: .\restart_and_continue_script.ps1
#>
param(
[switch]$PostRestart
)
$ErrorActionPreference = "Stop"
$taskName = "PostRestartScript"
#Creates a task scheduler entry to continue running the script at logon
function Set-StartupTask($script_path) {
$taskAction = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-executionpolicy bypass -noprofile -file $script_path -PostRestart"
$taskTrigger = New-ScheduledTaskTrigger -AtLogOn
Register-ScheduledTask -TaskName $taskName -RunLevel Highest -Action $taskAction -User $env:USERNAME -Trigger $taskTrigger -Force
}
function Remove-StartupTask {
$task = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue
if ($task -ne $null)
{
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false
}
}
# Get the ID and security principal of the current user account
$myWindowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent();
$myWindowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal($myWindowsID);
# Get the security principal for the Administrator role
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
# Check to see if we are currently running "as Administrator"
if ($myWindowsPrincipal.IsInRole($adminRole))
{
# We are running "as Administrator" - so change the title and background color to indicate this
$Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"
$Host.UI.RawUI.BackgroundColor = "DarkBlue"
clear-host
}
else
{
# We are not running "as Administrator" - so relaunch as administrator
# Create a new process object that starts PowerShell
$newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";
# Specify the current script path and name as a parameter
$newProcess.Arguments = $myInvocation.MyCommand.Definition;
# Indicate that the process should be elevated
$newProcess.Verb = "runas";
# Start the new process
[System.Diagnostics.Process]::Start($newProcess);
# Exit from the current, unelevated, process
exit
}
Set-Location (split-path -parent $PSCommandPath)
#Remove the task (if exists)
Remove-StartupTask
if (-not $PostRestart)
{
#RUN ANY CODE BEFORE RESTARTING HERE
#SETUP RESTART TASK THAT WILL RESUME THIS SCRIPT AT LOGON
Set-StartupTask $PSCommandPath
Read-Host -Prompt "Press ENTER to RESTART..." | Out-Null
Write-Host "Restarting"
Restart-Computer -Force
}
else
{
Write-Host "POST RESTART..."
#RUN ANY CODE AFTER RESTARTING HERE
Read-Host -Prompt "Done." | Out-Null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment