Public/Enable-UWFServicing.ps1

Function Enable-UWFServicing {
    <#
    .SYNOPSIS
        Enable UWF servicing
    .DESCRIPTION
        Enable UWF servicing after the next restart
    .INPUTS
        None
    .OUTPUTS
        Returns an HRESULT value that indicates WMI status or a WMI error.
    .EXAMPLE
        Enable-UWFServicing
    .LINK
        about_functions_advanced
    .LINK
        about_CommonParameters
    #>

    [CmdletBinding(
        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 (!$Script:UWFServicing) {
            $ExitCode = 424
            Throw "Unable to get handle to an instance of the UWF_Servicing class"
        }

        $nextSession = Get-WmiObject -Namespace $Script:UWFNameSpace -Class UWF_Servicing | Where-Object { $_.CurrentSession -eq $false }
        If ($nextSession) {
            $Enable = $nextSession.Enable() | Out-Null
            $ExitCode = $Enable.ReturnValue
        }
    }

    End {
        If ($Null -eq $ExitCode) {
            # 424 Failed Dependency
            $ExitCode = 424
        }
        If ($ExitCode -eq 0) {
            Write-Warning "This device is enabled for servicing mode after the next restart."
        } Else {
            Return $("{0:x0}" -f $ExitCode)
        }
    }
}