system-credential.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 |
# # system_credential.ps1 # # An array to store credentaials in memory (each object contains key,username,password) $script:credentialsStore = @() function New-StoredCredential { <# .SYNOPSIS Adds a credentials to the in memory credential store. .DESCRIPTION A user will be asked to provide a username and password. Credentials will be stored in memory table. The credentails will be stored as secure string. You shall call Save-Credentials to save in memory table to file. .EXAMPLE New-StoredCredential -Key 'SMTP Credentials' -Message 'Provide a credentials for SMTP server' #> [CmdletBinding()] param ( [parameter(Mandatory=$true)] [string]$Key, [parameter()] [string]$Username, [parameter()] [Security.SecureString]$Password = ( new-object SecureString ), [parameter()] [string]$Message="Please provide credentials:" ) $credentialsItem = Get-StoredCredential($Key) if( $credentialsItem -ne $null ) { Write-Warning "A key '$Key' already exist!" return } $user = $null $pwd = $null if( $Username -eq '' -and $Password.Length -eq 0) { $credentials = Get-Credential -Message $Message $user = $credentials.UserName | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString $pwd = $credentials.Password | ConvertFrom-SecureString } else { $user = $Username | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString $pwd = $Password | ConvertFrom-SecureString } $credentialsItem = New-Object PSObject $credentialsItem | Add-Member �MemberType NoteProperty �Name Key �Value $Key $credentialsItem | Add-Member �MemberType NoteProperty �Name Username �Value $user $credentialsItem | Add-Member �MemberType NoteProperty �Name Password �Value $pwd $script:credentialsStore += $credentialsItem; } function Export-CredentialStore { <# .SYNOPSIS Saves an in memory credentials table to the CSV file. .EXAMPLE Save-CredentialStore -Path 'C:\MyScriptsConfiguration' #> [CmdletBinding()] param ( [parameter(Mandatory=$true)] [string]$Path ) $script:credentialsStore | Export-Csv -Path $Path -NoTypeInformation } function Import-CredentialStore { <# .SYNOPSIS Loads credentials from a CSV file and stores credentials in a memory table. .EXAMPLE Load-CredentialStore -Path 'C:\MyScriptsConfiguration' #> [CmdletBinding()] param ( [parameter(Mandatory=$true)] [string]$Path ) $script:credentialsStore = @() $script:credentialsStore += Import-Csv -Path $Path } function Get-StoredCredential { [CmdletBinding()] param ( [parameter(Mandatory=$true)] [string]$Key ) return $script:credentialsStore | Where-Object { $_.Key -eq $Key } } function Get-StoredPSCredential { [CmdletBinding()] param ( [parameter(Mandatory=$true)] [string]$Key ) $credentials = $script:credentialsStore | Where-Object { $_.Key -eq $Key } if( $credentials -ne $null) { return New-Object System.Management.Automation.PSCredential ( (ConvertTo-PlainText -Secret $credentials.Username), (ConvertTo-SecureString $credentials.Password)) } return $null; } function Test-StoredCredential { [CmdletBinding()] param ( [parameter(Mandatory=$true)] [string]$Key ) $credentialsItem = Get-StoredCredential($Key) if( $credentialsItem -eq $null ) { return $false } return $true } function ConvertTo-PlainText { <# .SYNOPSIS Converts a secure string to the plain text .EXAMPLE ConvertTo-PlainText -String 'jsjdakdajskdajsd' #> [CmdletBinding()] param ( [parameter(Mandatory=$true)] [string]$Secret ) $secureString = $Secret | ConvertTo-SecureString $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR( $secureString ) return [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) } Export-ModuleMember -Function ConvertTo-PlainText Export-ModuleMember -Function New-StoredCredential Export-ModuleMember -Function Test-StoredCredential Export-ModuleMember -Function Get-StoredCredential Export-ModuleMember -Function Import-CredentialStore Export-ModuleMember -Function Export-CredentialStore Export-ModuleMember -Function Get-StoredPSCredential |