Public/Restart-UWFSystem.ps1

Function Restart-UWFSystem {
    <#
    .SYNOPSIS
        Safely restarts a system protected by UWF, even if the overlay is full.
    .DESCRIPTION
        Safely restarts a system protected by UWF, even if the overlay is full.
 
        You must use an administrator account to call this method.
 
        If the overlay is full, or near full, shutting down or restarting the system normally can cause the system to take an extremely long time to shut down. This occurs when the system repeatedly tries to write files during shutdown, which constantly fail due to the overlay being full. You can call this method to safely restart a system by avoiding this scenario.
 
        If the overlay becomes full while the system is performing a large amount of writes, such as copying a large group of files, calling this method can still result in a long shutdown time.
    .INPUTS
        None
    .OUTPUTS
        Returns an HRESULT value that indicates WMI status or a WMI error.
    .EXAMPLE
        Restart-UWFSystem
    .LINK
        about_functions_advanced
    .LINK
        about_CommonParameters
    #>

    [CmdletBinding(
        SupportsShouldProcess = $true,
        ConfirmImpact = "Medium"
    )]
    Param()

    Begin {
        If (-not $PSBoundParameters.ContainsKey('Verbose')) {
            $VerbosePreference = $PSCmdlet.SessionState.PSVariable.GetValue('VerbosePreference')
        }
        If (-not $PSBoundParameters.ContainsKey('WhatIf')) {
            $WhatIfPreference = $PSCmdlet.SessionState.PSVariable.GetValue('WhatIfPreference')
        }
        If (-not $PSBoundParameters.ContainsKey('Confirm')) {
            $ConfirmPreference = $PSCmdlet.SessionState.PSVariable.GetValue('ConfirmPreference')
        }
        If (-not $PSBoundParameters.ContainsKey('ErrorAction')) {
            $ErrorActionPreference = $PSCmdlet.SessionState.PSVariable.GetValue('ErrorActionPreference')
        }
    }

    Process {
        If ($PSCmdlet.ShouldProcess($env:COMPUTERNAME, "Restart System")) {
            If (!$Script:UWF) {
                $ExitCode = 424
                Throw "Unable to perform restart through Unified Write Filter."
            }
            $Disable = $Script:UWF.RestartSystem()
            $ExitCode = $Disable.ReturnValue
        }
    }

    End {
        If ($Null -eq $ExitCode) {
            # 424 Failed Dependency
            $ExitCode = 424
        }
        If ($ExitCode -eq 0) {
            Write-Warning "$env:COMPUTERNAME will restart."
        }
        Return $("{0:x0}" -f $ExitCode)
    }
}