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.

Revisions

  1. IvanDrag0 created this gist Apr 19, 2024.
    34 changes: 34 additions & 0 deletions Reboot.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    # 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
    }