Functions/Environment.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
function Get-PowerShellVersion
{
    # accessing the value indirectly so it can be mocked
    (Get-Variable 'PSVersionTable' -ValueOnly).PsVersion.Major
}

function Get-OperatingSystem
{
    # Prior to v6, PowerShell was solely on Windows. In v6, the $IsWindows variable was introduced.
    if ((Get-PowerShellVersion) -lt 6)
    {
        'Windows'
    }
    elseif (Get-Variable -Name 'IsWindows' -ErrorAction 'SilentlyContinue' -ValueOnly )
    {
        'Windows'
    }
    elseif (Get-Variable -Name 'IsOSX' -ErrorAction 'SilentlyContinue' -ValueOnly )
    {
        'OSX'
    }
    elseif (Get-Variable -Name 'IsLinux' -ErrorAction 'SilentlyContinue' -ValueOnly )
    {
        'Linux'
    }
    else
    {
        throw "Unsupported Operating system!"
    }
}

function Get-TempDirectory
{
    if ((Get-OperatingSystem) -eq 'Windows')
    {
        $env:TEMP
    }
    else
    {
        '/tmp'
    }
}