Functions/machine.ps1

function Grant-PowershellAsAdmin(
    [Parameter(Mandatory = $true)]
    [ValidateNotNullOrEmpty()]
    $path,
    
    [string[]]$scriptParams
) {
    if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
        Invoke-PowerShellScript $path $scriptParams
        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
    }
}

$Script:alreadyResumed = $false
function Resume-AndContinue(
    [Parameter(Mandatory = $true, Position = 0)]
    [ValidateNotNullOrEmpty()]
    [string]$startingStep,
    
    [Parameter(Mandatory = $true, Position = 1)]
    [ValidateNotNullOrEmpty()]
    [string] $prospectStep
) {
    if ($startingStep -eq $prospectStep -or $Script:alreadyResumed ) {
        $Script:alreadyResumed = $true
    }
    return $Script:alreadyResumed
}

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
}