$StoredCredentials = "$Home\\Documents\\WindowsPowerShell\\Credentials"
if(-not (Test-Path -Path $StoredCredentials)) {
New-Item -Path $StoredCredentials -ItemType Directory -Force | Out-Null
}
if(-not (Test-Path -Path \'AuthStore:\')) {
New-PSDrive -Name \'AuthStore\' -PSProvider FileSystem -Root $StoredCredentials -Scope global | Out-Null
}
function Export-Credential {
Param (
[Parameter(Mandatory)]
[System.Management.Automation.PSCredential]
$Credential,
[Parameter(Mandatory)]
[System.String]
$LookupKey
)
$FullName = $( \'AuthStore:\\{0}\' -f $LookupKey )
if(Test-Path -Path $FullName) { Write-Warning -Message "Updateing Credential Associated with Key $LookupKey" }
$CredentialCopy = $Credential | Select-Object *
$CredentialCopy.Password = $CredentialCopy.Password | ConvertFrom-SecureString
$CredentialCopy | ConvertTo-Json | Set-Content -Path $FullName -Force
}
function Import-Credential {
param (
[Parameter(Mandatory=$true)]
[System.String]
$LookupKey
)
$FullName = $( \'AuthStore:\\{0}\' -f $LookupKey )
if(-not (Test-Path -Path $FullName)) {
Write-Error -Message \'No Credential Found\'
return
}
$CredentialCopy = Get-Content -Path $FullName -Raw | ConvertFrom-Json
$CredentialCopy.password = $CredentialCopy.Password | ConvertTo-SecureString
New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $CredentialCopy.username, $CredentialCopy.password
}