# This PS module contains functions for Desired State Configuration (DSC) "Environment" provider

# Fallback message strings in en-US
DATA localizedData
{
    # culture = "en-US"
    ConvertFrom-StringData @'        
        EnvVarCreated = (CREATE) Environment variable '{0}' with value '{1}'        
        EnvVarSetError = (ERROR) Failed to set environment variable '{0}' to value '{1}'
        EnvVarPathSetError = (ERROR) Failed to add path '{0}' to environment variable '{1}' holding value '{2}'
        EnvVarRemoveError = (ERROR) Failed to remove environment variable '{0}' holding value '{1}'
        EnvVarPathRemoveError = (ERROR) Failed to remove path '{0}' from variable '{1}' holding value '{2}'
        EnvVarUnchanged = (UNCHANGED) Environment variable '{0}' with value '{1}'
        EnvVarUpdated = (UPDATE) Environment variable '{0}' from value '{1}' to value '{2}'
        EnvVarPathUnchanged = (UNCHANGED) Path environment variable '{0}' with value '{1}'
        EnvVarPathUpdated = (UPDATE) Environment variable '{0}' from value '{1}' to value '{2}'        
        EnvVarNotFound = (NOT FOUND) Environment variable '{0}'
        EnvVarFound = (FOUND) Environment variable '{0}' with value '{1}'
        EnvVarFoundWithMisMatchingValue = (FOUND MISMATCH) Environment variable '{0}' with value '{1}' mismatched the specified value '{2}'        
        EnvVarRemoved = (REMOVE) Environment variable '{0}'
'@
}
Import-LocalizedData  LocalizedData -filename MSFT_EnvironmentResource.strings.psd1

 
#-------------------------------------
# Script-level Constants and Variables
#-------------------------------------
$EnvVarRegPathMachine = "HKLM:\\System\\CurrentControlSet\\Control\\Session Manager\\Environment"
$EnvVarRegPathUser = "HKCU:\\Environment"

$EnvironmentVariableTarget = @{ Process = 0; User = 1; Machine = 2 }
$MaxSystemEnvVariableLength = 1024
$MaxUserEnvVariableLength = 255

Function Throw-InvalidArgumentException
{
    param(
        [string] $Message,
        [string] $ParamName
    )
    
    $exception = new-object System.ArgumentException $Message,$ParamName
    $errorRecord = New-Object System.Management.Automation.ErrorRecord $exception,$ParamName,"InvalidArgument",$null
    throw $errorRecord
}

function GetEnvironmentVariable
{
    param
    (
        [parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String] $Name, 

        [parameter(Mandatory = $true)]
        [int] $Target
    )

    if ($Target -eq $EnvironmentVariableTarget.Process) 
    {
        return [System.Environment]::GetEnvironmentVariable($Name);
    }

    if ($Target -eq $EnvironmentVariableTarget.Machine)
    {
        $retVal = Get-ItemProperty $EnvVarRegPathMachine -Name $Name -ErrorAction SilentlyContinue
        return $retVal.$Name
    }

    if ($Target -eq $EnvironmentVariableTarget.User)
    {
        $retVal = Get-ItemProperty $EnvVarRegPathUser -Name $Name -ErrorAction SilentlyContinue
        return $retVal.$Name
    }
}

function SetEnvironmentVariable
{
    param
    (
        [parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String] $Name, 

        [String] $Value,

        [parameter(Mandatory = $true)]
        [int] $Target
    )

    if ($Target -eq $EnvironmentVariableTarget.Process) 
    {
        [System.Environment]::SetEnvironmentVariable($Name, $Value);
    }

    if ($Target -eq $EnvironmentVariableTarget.Machine) 
    {
        if ($Name.Length -ge $MaxSystemEnvVariableLength) {
            Throw-InvalidArgumentException -Message "Argument is too long." -ParamName $Name
        }
        $Path = $EnvVarRegPathMachine
    }
    elseif ($Target -eq $EnvironmentVariableTarget.User) 
    {
        if ($Name.Length -ge $MaxUserEnvVariableLength) {
            Throw-InvalidArgumentException -Message "Argument is too long." -ParamName $Name
        }
        $Path = $EnvVarRegPathUser
    }

    $environmentKey = Get-ItemProperty $Path -Name $Name -ErrorAction S