ETTING_NAME%%

Set-StrictMode -Version latest

Set-Variable -name SET -value "Set" -option constant
Set-Variable -name CLEAR -value "CLEAR" -option constant
Set-Variable -name UNMANAGED -value "UNMANAGED" -option constant
Set-Variable -name NO_ERROR -value 0 -option constant
Set-Variable -name UNSPECIFIED_ERROR -value 1 -option constant
Set-Variable -name REMEDIATION_FAILED -value 1 -option constant

Set-Variable -name HIVE -value %%HIVE%% 
Set-Variable -name NAMESPACE -value "root\Microsoft\UEV"
Set-Variable -name SETTING -value "%%SETTING_NAME%%"
Set-Variable -name DESIRED_VALUE -value "%%SETTING_VALUE%%"
Set-Variable -name DESIRED_STATE -value "%%DESIRED_STATE%%"

$result = $NO_ERROR

if ($HIVE -eq "HKLM")
{
    $currentHive = "ComputerConfiguration"
}
else
{
    $currentHive = "UserConfiguration"
}

if ($DESIRED_STATE -eq $SET)
{
    # Updates setting to desired value

    try
    {
        $config = Get-WmiObject -Namespace $NAMESPACE $currentHive
        $config.$SETTING = ($DESIRED_VALUE -eq "true")
        [void]$config.Put()
    }
    catch
    {
        write-host "Failed to set UE-V %%SETTING_NAME%%: " + $_;
        $result = $REMEDIATION_FAILED
    }
}
elseif ($DESIRED_STATE -eq $CLEAR)
{
    # Removes setting

    try
    {
        $config = Get-WmiObject -Namespace $NAMESPACE $currentHive
        $config.$SETTING = $null
        [void]$config.Put()
    }
    catch
    {
        write-host "Failed to clear UE-V %%SETTING_NAME%%: " + $_;
        $result = $REMEDIATION_FAILED
    }
}
elseif ($DESIRED_STATE -eq $UNMANAGED)
{
    # This should never get called because the discovery script should prevent it
    $result = $UNSPECIFIED_ERROR
}
    
return $result
