Public/Add-MDSCredential.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
Function Add-MDSCredential {
    <#
    .SYNOPSIS
    Add a credential entry to the MDSCredentials file

    .DESCRIPTION
    Add a credential entry to the MDSCredentials file. Supply the name of the entry and be prompted for a username and password.

    .EXAMPLE
    Add-MDSCredential -Name MyCred1

    Create a credential entry named 'MyCred1'

    .EXAMPLE
    Add-MDSCredential -Name MyCred1 -UserName 'ASmith'

    Create a credential entry named 'MyCred1'

    .NOTES

    #>

    [cmdletbinding()]
    Param (
        [parameter(Position=0, Mandatory=$True)]
        [ValidateNotNullOrEmpty()]
        [string]$Name,

        [Parameter()]
        [string]$UserName
    )

    Begin {
        Try {
            $Hash = Get-MDSCredential -SortByName:$false -ErrorAction Stop
        }
        Catch {
            $PSCmdlet.ThrowTerminatingError($PSItem)
        }
        If (-not $Hash) {$Hash = @{}}
    }
    Process {
        Try {
            $getCredentialSplat = @{
                Message     = "Credentials will be stored as $Name in the MDSTools credential store"
                ErrorAction = 'Stop'
            }
            If ($null -ne $PSBoundParameters.UserName) {
                $getCredentialSplat.Add('UserName',$UserName)
            }
            $Credentials = Get-Credential @getCredentialSplat
            $Username = $Credentials.UserName
            $Password = $Credentials.Password | ConvertFrom-SecureString

            $Hash.Add($PSBoundParameters.Name,@($UserName,$Password))
            $Username = $Password = $null
            Write-Verbose "Added credential record $($Name)"
            $Hash | Export-CliXML $CredentialFilePath
        }
        Catch [System.Management.Automation.MethodInvocationException] {
            $Message = "A record for {0} already exists. Use Update-MDSCredential to edit a record." -f $Name
            Write-Error -Message $Message -ErrorAction Stop -Exception ([System.Management.Automation.MethodInvocationException]::new())
        }
        Catch {
            Write-Error $PSItem
        }
    }
    End {}
}