Functions/machine.ps1

function Invoke-RunPowershellAsAdmin {
    if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { 
        Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit
    }
}

function Wait-ForKeypress ([string]$message = "Press any key to continue…", [bool] $shouldExit = $false) {
    if ($psISE) {
        $Shell = New-Object -ComObject "WScript.Shell"
        $null = $Shell.Popup($message, 0, "Script Paused", 0)
    }
    elseif ($host.Name -cmatch "ConsoleHost") {
        Write-Host "$message" -ForegroundColor Yellow
        $null = $host.ui.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    }
    else {
        Write-Host "$Message`nCurrently is running in '$($host.Name)' and not running in 'ConsoleHost', so sleep for 3 seconds"
        Start-Sleep -s 3
    }
    if ($shouldExit) {
        exit
    }
}

$started = $false
function Should-RunStep(
    [Parameter(Mandatory = $true, Position = 0)]
    [ValidateNotNullOrEmpty()]
    [string]$startingStep,
    
    [Parameter(Mandatory = $true, Position = 1)]
    [ValidateNotNullOrEmpty()]
    [string] $prospectStep
) {
    if ($startingStep -eq $prospectStep -or $started) {
        $started = $true
    }
    return $started
}

function Restart-AndResume(
    [Parameter(Mandatory = $true, Position = 0)]
    [ValidateNotNullOrEmpty()]
    [string] $script,
    
    [Parameter(Mandatory = $true, Position = 1)]
    [ValidateNotNullOrEmpty()][string] $step,
    
    [string] $paramName = "step") {
    $powerShellPath = "$env:windir\system32\WindowsPowerShell\v1.0\powershell.exe"
    $RegRunKey = "SSV-Core:Restart-And-Resume"
    $value = "$powerShellPath $script -${paramName} $step"
    Set-LocalMachineRunOnceVar $RegRunKey $value
    Restart-Computer
    exit
}