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")) {
        Write-Warning "Switching powershell to runAs Admin"
        Invoke-PowerShellScript $path $scriptParams
        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,
    
    [AllowNull()]
    [AllowEmptyString()]
    [Parameter(Position = 1)]
    [string[]]$scriptParams = @()) {
    $powerShellPath = "$env:windir\system32\WindowsPowerShell\v1.0\powershell.exe"
    $RegRunKey = "SSV-Core:Restart-And-Resume"
    $params = Convert-ArrayToStringParams $scriptParams
    $value = "$powerShellPath $script $params"
    Set-LocalMachineRunOnceVar $RegRunKey $value
    Restart-Computer
    exit
}