Public/Disable-UWFServicing.ps1

Function Disable-UWFServicing {
    <#
    .SYNOPSIS
        Disable UWF servicing
    .DESCRIPTION
        Disable UWF servicing after the next restart
    .INPUTS
        None
    .OUTPUTS
        Returns an HRESULT value that indicates WMI status or a WMI error.
    .EXAMPLE
        Disable-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) {
            $Disable = $nextSession.Disable() | Out-Null
            $ExitCode = $Disable.ReturnValue
        }
    }

    End {
        If ($Null -eq $ExitCode) {
            # 424 Failed Dependency
            $ExitCode = 424
        }
        If ($ExitCode -eq 0) {
            Write-Output "Servicing mode is now disabled for this device."
        } Else {
            Return $("{0:x0}" -f $ExitCode)
        }
    }
}