PasswordManagement.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269

using namespace Microsoft.PowerShell.SecretManagement

Set-StrictMode -Version 3

$prefix = "imm"
$prefixLength = $prefix.Length + 1


function aSecretVaultIsAvailable {
    [OutputType([Boolean])]
    param()

    $vaultAvailable = $false
    try {
        # if this works we know we have a registered vault
        $secretVault = Get-SecretVault

        # let's see if we can import the module for the vault
        $module = Import-Module -Name $secretVault.ModuleName -EA SilentlyContinue -PassThru

        if ($module) {
            $vaultAvailable = $true
        }

    }
    catch {
    }

    if (!$vaultAvailable) {
        Write-Warning "There are currently no secret vaults available."
    }
    $vaultAvailable
}


function Get-SavedMetasysUsers {
    <#
    .SYNOPSIS
        Finds and returns user names associated with saved Metasys credentials.

    .DESCRIPTION
        The Invoke-MetasysMethod module includes functions that may update a secret vault. This cmdlet finds and returns the user names of Metasys accounts for a host that matches the provided 'MetasysHost'. If no 'MetasysHost' parameter argument is provided then all user names for all hosts will be returned.

        Note: This cmdlet only returns user names associated with Metasys credentials that have been added using this or other cmdlets of Invoke-MetasysMethod module.

    .OUTPUTS
        The list of matching user names.

    .EXAMPLE
        PS > Get-SavedMetasysUsers -MetasysHost adx55

        Gets the list of users that have been saved for the site host adx55

    #>

    param (
        # A hostname or ip address.
        #
        # Aliases: -h, -host, -SiteHost
        [Alias("h", "host", "SiteHost")]
        [string]$MetasysHost
    )

    if (!(aSecretVaultIsAvailable)) {
        return
    }

    $searchFor = ($MetasysHost) ? "$prefix`:$metasysHost`:*" : "$prefix`:*"

    $secretInfo = Get-SecretInfo -Name $searchFor -ErrorAction SilentlyContinue

    if (!$secretInfo) {
        return
    }

    if ($MetasysHost) {
        $UserName = @{label = "UserName"; expression = { ($_.Name -split ":")[2] } }
        $HostName = @{label = "Host"; expression = { ($_.Name -split ":")[1] } }
        if ($secretInfo -is [Object[]]) {
            return $secretInfo | Select-Object $HostName, $UserName | Where-Object { $_.Host -eq $MetasysHost } | Select-Object $UserName
        }
        else {
            return $secretInfo | Select-Object $UserName
        }
    }
    else {
        $userNameExpression = {
            $lastColon = $_.Name.LastIndexOf(":")
            if ($lastColon -lt 0) {
                return $null
            }
            return $_.Name.Substring($lastColon + 1)
        }
        $metasysHostExpression = {

            $firstColon = $_.Name.IndexOf(":")
            $lastColon = $_.Name.LastIndexOf(":")

            if ($firstColon -eq $lastColon) {
                return $null
            }

            return $_.Name.Substring($firstColon + 1, $lastColon - $firstColon - 1)
        }

        $userNameSelector = @{label = "UserName"; expression = $userNameExpression }
        $hostSelector = @{label = "Host"; expression = $metasysHostExpression }

        $regex = [System.Text.RegularExpressions.Regex]::new("^${prefix}:[^:]+:[^:]+$")

        return $secretInfo | Where-Object { $regex.Match($_.Name).Success } |  Select-Object $hostSelector, $userNameSelector #| Where-Object { $null -ne $_.UserName } | Where-Object { $null -ne $_.MetasysHost }


    }


}

function Get-SavedMetasysPassword {
    <#
    .SYNOPSIS
        Finds and returns the password associated with saved Metasys credentials.

    .DESCRIPTION
        The Invoke-MetasysMethod module includes functions that may update a secret vault. This cmdlet finds and returns the password of a Metasys account for a host that matches the provided 'MetasysHost' and a user that matches 'UserName'.

        The password is returned as a SecureString object unless the '-AsPlainText' parameter switch is used, in which case the password is returned in plain text.

    .OUTPUTS
        System.Object

    .EXAMPLE
        PS > Get-SavedMetasysPassword -MetasysHost adx55 -UserName fred
        System.Security.SecureString

        Gets the password for fred on host adx55

    .EXAMPLE
        PS > Get-SavedMetasysPassword -MetasysHost adx55 -UserName fred -AsPlainText
        PlainTextPassword

        Gets the password for fred on host adx55 and returns it as plain text

    #>

    param (
        # A hostname or ip address.
        #
        # Aliases: -h, -host, -SiteHost
        [Alias("h", "host", "SiteHost")]
        [Parameter(Mandatory = $true)]
        [string]$MetasysHost,

        # The username of an account on the host
        #
        # Alias: -u
        [Alias("u")]
        [Parameter(Mandatory = $true)]
        [string]$UserName,
        # Return the password back as plain text
        [switch]$AsPlainText
    )

    if (!(aSecretVaultIsAvailable)) {
        return
    }

    $secretInfo = Get-SecretInfo -Name "${prefix}:${MetasysHost}:$UserName" -ErrorAction SilentlyContinue

    if (!$secretInfo) {
        return
    }

    if ($secretInfo -is [System.Object[]]) {
        $secretInfo = $secretInfo[0]
    }

    $secret = Get-Secret -Name $secretInfo.Name -Vault $secretInfo.VaultName

    if ($AsPlainText.IsPresent) {
        return $secret | ConvertFrom-SecureString -AsPlainText
    }

    return $secret
}

function Remove-SavedMetasysPassword {
    <#
    .SYNOPSIS
        Finds and removes the password associated with saved Metasys
        credentials.

    .DESCRIPTION
        The Invoke-MetasysMethod module includes functions that may update a secret vault. This cmdlet finds and removes the Metasys credentials for a host that matches the provided 'MetasysHost' and a user that matches 'UserName'.

    .EXAMPLE
        PS > Remove-SavedMetasysPassword -MetasysHost adx55 -UserName fred

        Finds and removes the password for fred on adx55

    #>

    param(
        # A hostname or ip address.
        #
        # Aliases: -h, -host, -SiteHost
        [Alias("h", "host", "SiteHost")]
        [Parameter(Mandatory = $true)]
        [string]$MetasysHost,

        # The username of an account on the host
        #
        # Alias: -u
        [Alias("u")]
        [Parameter(Mandatory = $true)]
        [string]$UserName
    )
    if (!(aSecretVaultIsAvailable)) {
        return
    }

    Get-SecretInfo -Name "${prefix}:${MetasysHost}:$UserName" -ErrorAction SilentlyContinue | ForEach-Object { Remove-Secret -Name $_.Name }

}

function Set-SavedMetasysPassword {
    <#
    .SYNOPSIS
        Saves Metasys credentials

    .DESCRIPTION
        This cmdlet saves Metasys credentials into the default secret vault

    .EXAMPLE
        PS > Set-SavedMetasysPassword -MetasysHost adx55 -UserName fred -Password $password

        Assuming $password is a SecureString that contains the password, this example saves fred's password for adx55.

    #>

    param(
        # A hostname or ip address.
        #
        # Aliases: -h, -host, -SiteHost
        [Alias("h", "host", "SiteHost")]
        [Parameter(Mandatory = $true)]
        [string]$MetasysHost,

        # The username of an account on the host
        #
        # Alias: -u
        [Alias("u")]
        [Parameter(Mandatory = $true)]
        [string]$UserName,

        # The password of an account on the host. Note: `Password` takes a
        # `SecureString`
        #
        # Alias: -p
        [Alias("p")]
        [Parameter(Mandatory = $true)]
        [SecureString]$Password
    )
    if (!(aSecretVaultIsAvailable)) {
        return
    }

    Set-Secret -Name "${prefix}:${MetasysHost}:$UserName" -SecureStringSecret $Password
}

Export-ModuleMember -Function "Get-SavedMetasysUsers", "Get-SavedMetasysPassword", "Remove-SavedMetasysPassword", "Set-SavedMetasysPassword"