functions/path/Set-PSUPath.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
function Set-PSUPath
{
<#
    .SYNOPSIS
        Detects the alias that called it and sets the location to the corresponding path found in the configuration system.
 
    .DESCRIPTION
        Detects the alias that called it and sets the location to the corresponding path.
        This function will normally be called using an alias that gets set by using Set-PSUPathAlias.
 
    .PARAMETER Alias
        This is the name of the alias that called the command.
        Default Value is $MyInvocation.InvocationName and is detected automatically
 
    .PARAMETER EnableException
        Replaces user friendly yellow warnings with bloody red exceptions of doom!
        Use this if you want the function to throw terminating errors you want to catch.
 
    .EXAMPLE
        PS C:\> Software
        PS C:\Software>
 
        In this example 'Software' is an alias for Set-PSUPath that was created by using Set-PSUPathAlias.
        Set-PSUPath detected that 'Software' was the alias that called it and then sends it to the path.
        It receives the path from Get-PSUPathAlias 'software'
#>

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
    [CmdletBinding()]
    param (
        [string]
        $Alias = $MyInvocation.InvocationName,
        
        [switch]
        $EnableException
    )
    
    begin
    {
        function Resolve-PsuPath
        {
            [OutputType([System.String])]
            [CmdletBinding()]
            param (
                [string]
                $Path
            )
            
            $environ = @{}
            foreach ($item in Get-ChildItem env:)
            {
                $environ[$item.Name] = $item.Value
            }
            $pattern = '%{0}%' -f ($environ.Keys -join '%|%')
            $replacement = {
                param (
                    [string]
                    $Match
                )
                $environ = @{ }
                foreach ($item in Get-ChildItem env:)
                {
                    $environ[$item.Name] = $item.Value
                }
                $environ[$Match.Trim('%')]
            }
            [regex]::Replace($Path, $pattern, $replacement, 'IgnoreCase')
        }
    }
    process
    {
        try
        {
            $psfConfigPath = Get-PSFConfigValue -FullName psutil.pathalias.$Alias -NotNull
        }
        
        catch
        {
            $paramStopPSFFunction = @{
                Message            = "Unable to find a path setting for the alias"
                Category        = 'InvalidOperation'
                Tag                = 'fail'
                ErrorRecord        = $_
                EnableException = $EnableException
            }
            
            Stop-PSFFunction @paramStopPSFFunction
            return
        }
        
        try
        {
            Set-Location (Resolve-PsuPath -Path $psfConfigPath) -ErrorAction Stop
        }
        catch
        {
            $psfFuncParams = @{
                EnableException = $EnableException
                Message            = "Unable to set location to $psfConfigPath"
                Category        = 'InvalidOperation'
                Tag                = 'fail'
                ErrorRecord        = $_
            }
            Stop-PSFFunction @psfFuncParams
            return
        }
    }
}