functions/path/Set-PSUPathAlias.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
function Set-PSUPathAlias
{
<#
    .SYNOPSIS
        Used to create an an alias that sets your location to the path you specify.
     
    .DESCRIPTION
        A detailed description of the Set-PSUPathAlias function.
     
    .PARAMETER Alias
        Name of the Alias that will be created for Set-PSUPath.
        Set-PSU Path detects the alias that called it and then finds the corresponding PSFConfig entry for it.
     
    .PARAMETER Path
        This is the path that you want your location to change to when the alias is called.
     
    .PARAMETER Register
        Causes PSUtil to remember the alias across sessions.
        For more advanced options, see Register-PSFConfig.
     
    .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:\> Set-PSUPathAlias -Alias 'work' -Path 'C:\work'
        Creates an alias to Set-PSUPath that will set the location to 'c:\work'
     
    .EXAMPLE
        PS C:\> Set-PSUPathAlias -Alias 'repos' -Path 'C:\repos' -Register
         
        Creates an alias for repos and registers the setting so that it will persist between sessions.
#>

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
    [CmdletBinding()]
    param
    (
        [Parameter(Position = 0, Mandatory)]
        [string]
        $Alias,
        
        [Parameter(Position = 1, Mandatory)]
        [string]
        $Path,
        
        [switch]
        $Register,
        
        [switch]
        $EnableException
    )
    
    try
    {
        Set-PSFConfig -FullName psutil.pathalias.$Alias -Value $Path -Description 'Sets an alias for Set-PSUPath that takes you to the path specified in the value.'
    }
    catch
    {
        $stopParams = @{
            Message            = 'Error encountered. Alias not set'
            Category        = 'InvalidOperation'
            Tag                = 'Fail'
            ErroRecord        = $_
            EnableException = $EnableException
        }
        Stop-PSFFunction @stopParams
        return
    }
    
    if ($Register)
    {
        Get-PSFConfig -FullName psutil.pathalias.$Alias | Register-PSFConfig
    }
    
    try
    {
        Import-PSUAlias -Name $Alias -Command Set-PSUPath
    }
    catch
    {
        $stopParams = @{
            Message            = 'Error. Alias not set'
            Category        = 'InvalidOperation'
            Tag                = 'Fail'
            ErroRecord        = $_
            EnableException = $EnableException
        }
        Stop-PSFFunction @stopParams
        return
    }
}