Wednesday, February 25, 2015

PowerShell - Get, Set and Remove environment variables


The environment provider made it easy to work with the environment variables on a local machine or a remote server to load the environment as a PSDrive and work on it. This simplifies accessing data from environment variables using PowerShell using the same cmdlets as working with files and directories like Get-Item, Set-Item, Remove-Item etc.
We'll make use of this feature to get, set and remove environment variables.

Function Test-EnvironmentPath
{
      [CmdletBinding()]
      param
      (
            [Parameter(Mandatory = $true)]
            [ValidateNotNullOrEmpty()]
            [String]$Path
      )
     
      if(-not ($Path.StartsWith("Env:\")))
      {
            $Path = "Env:\$($Path)"
      }
      Test-Path $Path   
}

Function Get-EnvironmentPath
{
      [CmdletBinding()]
      param
      (
            [Parameter(Mandatory = $true)]
            [ValidateNotNullOrEmpty()]
            [String]$Path
      )
   
      if(-not ($Path.StartsWith("Env:\")))
      {
            $Path = "Env:\$($Path)"
      }
      if(Test-EnvironmentPath -Path $Path)
      {
            (Get-Item $Path).Value
      }   
      else
      {
            $null
      }
}

Function Set-EnvironmentPath
{
      [CmdletBinding()]
      param
      (
            [Parameter(Mandatory = $true)]
            [ValidateNotNullOrEmpty()]
            [String]$Path,
           
            [Parameter(Mandatory = $true)]
            [ValidateNotNullOrEmpty()]
            [String]$Value
      )
      if(-not ($Path.StartsWith("Env:")))
      {
            $Path = "Env:$($Path)"
      }
      Set-Item -Path $Path -Value $Value
}

Function Remove-EnvironmentPath
{
      [CmdletBinding()]
      param
      (
            [Parameter(Mandatory = $true)]
            [ValidateNotNullOrEmpty()]
            [String]$Path
      )
      if(-not ($Path.StartsWith("Env:\")))
      {
            $Path = "Env:\$($Path)"
      }
      if(Test-EnvironmentPath -Path $Path)
      {
            Remove-Item $Path
      }
}

To test the scripts I’ve used the FluentShellUnit project and have my test cases as
[TestMethod]
[TestCategory("xPowerPack Module")]
public void TestEnvironmentPath_returns_true_if_value_exists()
{
    var expected = !String.IsNullOrEmpty(Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine));

    var actual = PsFactory.Create(HostState.Core)
        .Load(@"Modules\Environment.psm1")
        .Execute("Test-EnvironmentPath", new Dictionary<string, string>
        {
            {"Path", "Path"}
        })
        .FirstResultItemAs<bool>();
    Assert.IsTrue(actual == expected);
}

[TestMethod]
[TestCategory("xPowerPack Module")]
public void GetEnvironmentPath_returns_value_from_environment_variable()
{
    var expected = Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine);

    var actual = PsFactory.Create(HostState.Core)
        .Load(@"Modules\Environment.psm1")
        .Execute("Get-EnvironmentPath", new Dictionary<string, string>
        {
            {"Path", "Path"}
        })
        .FirstResultItemAs<string>();
    Assert.IsTrue(actual == expected);
}

No comments: