Public/Get-PSSlackConfig.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
Function Get-PSSlackConfig {
    <#
    .SYNOPSIS
        Get PSSlack module configuration.
 
    .DESCRIPTION
        Get PSSlack module configuration
 
    .PARAMETER Source
        Get the config data from either...
 
            PSSlack: the live module variable used for command defaults
            PSSlack.xml: the serialized PSSlack.xml that loads when importing the module
 
        Defaults to PSSlack
 
    .PARAMETER Path
        If specified, read config from this XML file.
 
        Defaults to PSSlack.xml in the module root
 
    .FUNCTIONALITY
        Slack
    #>

    [cmdletbinding(DefaultParameterSetName = 'source')]
    param(
        [parameter(ParameterSetName='source')]
        [ValidateSet("PSSlack","PSSlack.xml")]
        $Source = "PSSlack",

        [parameter(ParameterSetName='path')]
        [parameter(ParameterSetName='source')]
        $Path = "$ModuleRoot\$env:USERNAME-$env:COMPUTERNAME-PSSlack.xml"
    )

    if($PSCmdlet.ParameterSetName -eq 'source' -and $Source -eq "PSSlack" -and -not $PSBoundParameters.ContainsKey('Path'))
    {
        $Script:PSSlack
    }
    else
    {
        function Decrypt {
            param($String)
            if($String -is [System.Security.SecureString])
            {
                [System.Runtime.InteropServices.marshal]::PtrToStringAuto(
                    [System.Runtime.InteropServices.marshal]::SecureStringToBSTR(
                        $string))
            }
        }
        Import-Clixml -Path $Path |
            Select-Object -Property ArchiveUri,
                                    @{l='Uri';e={Decrypt $_.Uri}},
                                    @{l='Token';e={Decrypt $_.Token}},
                                    Proxy
    }

}