Skip to content

Instantly share code, notes, and snippets.

@IvanDrag0
Created April 19, 2024 17:30
Show Gist options
  • Select an option

  • Save IvanDrag0/cc6b466870dabd931ccf0fc59b6bb385 to your computer and use it in GitHub Desktop.

Select an option

Save IvanDrag0/cc6b466870dabd931ccf0fc59b6bb385 to your computer and use it in GitHub Desktop.
PowerShell Reboot and Continue
# From https://github.com/PhilipRieck/MachineSetup/blob/main/Modules/Reboots.psm1
$script:RebootRequired = $false
function SetRebootRequired($value = $true){
$script:RebootRequired = $value
}
function IsRebootRequired(){
return $script:RebootRequired
}
function Remove-Reboot()
{
$currentTasks = Get-ScheduledTask -TaskName "LtiMachineSetup" -ErrorAction Ignore
foreach($task in $currentTasks){
Unregister-ScheduledTask -TaskName $task.TaskName -Confirm:$false -ErrorAction Ignore
}
}
function Invoke-RebootAndContinue([string]$scriptToRun){
Remove-Reboot
$currentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$taskName = "LtiMachineSetup"
$fileName = $scriptToRun
$taskAction = New-ScheduledTaskAction -Execute "pwsh.exe" -Argument "-File $fileName"
$taskTrigger = New-ScheduledTaskTrigger -AtLogOn -User $currentUser
$taskPrincipal = New-ScheduledTaskPrincipal -UserId $currentUser -RunLevel Highest
Register-ScheduledTask -Action $taskAction -Trigger $taskTrigger -TaskName $taskName -Description "MachineSetup For Lti" -Force -Principal $taskPrincipal
write-host "Rebooting to continue setup. Please log back in to continue."
$x = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
Restart-Computer -Force
exit 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment