Skip to content

Instantly share code, notes, and snippets.

@rufflabs
Created May 23, 2015 14:33
Show Gist options
  • Save rufflabs/71827e64e4dd5dc64b4d to your computer and use it in GitHub Desktop.
Save rufflabs/71827e64e4dd5dc64b4d to your computer and use it in GitHub Desktop.
Stores a username/password PSCredential in an XML file
<#
.SYNOPSIS
Stores a username/password PSCredential in an XML file.
.DESCRIPTION
Store-Credentials.ps1 will prompt for a username/password via Get-Credential. It
will then export that object into an XML file via Export-CliXml.
To retrieve the stored credentials:
$Credentials = Import-CliXml <Path to XML file>
.NOTES
The credentials stored in this manner are encrypted, and can only be retrieved
by the same Windows user account that initially stores them. When you use this
script, make sure you are logged in as the user account that will later use
these stored credentials.
.PARAMETER Path
The filename to export the credentials to.
.EXAMPLE
.\Store-Credentials.ps1 C:\StoredCredentials.xml
Prompt for a username and password, and save them to the specified XML file.
#>
param(
[Parameter(Mandatory=$True,Position=1)][String]$Filename
)
function Verify-Path {
<#
.SYNOPSIS
Checks that the path exists, and prompts to create it if needed. Returns
the filename and path as a string, such as: 'c:\folder\file.txt'
.DESCRIPTION
If the specified path exists, this returns the path. If it doesn't exist it
will prompt the user to create it. If the user does not want to create that
folder, it will continue prompting for a path until a valid path is given
or created.
.PARAMETER Path
A filename path string, ie: C:\folder\file.txt
#>
param(
[Parameter(Mandatory=$True)][String]$Path
)
if(-Not (Test-Path (Split-Path $Path))) {
$Yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes",`
"Creates the specified folder."
$No = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
"Does not create the folder, and prompts for an alternate location to store the file."
$Options = [System.Management.Automation.Host.ChoiceDescription[]]($Yes,$No)
$Selection = $Host.UI.PromptForChoice("Create folder: $(Split-Path $Path)",`
"The specified path does not exist, would you like to create it?", $Options, 0)
switch($Selection) {
0 {
# Create the folder structure
New-Item -Path (Split-Path $Path) -ItemType Directory -Force
Return $Path
}
1 {
# Prompt for an alternate location
$AlternatePath = Read-Host "Please provide the path, including filename (ie: c:\test.xml)"
$Path = Verify-Path $AlternatePath
Return $Path
}
}
} else {
# This folder structure already exists
Return $Path
}
}
$Credentials = Get-Credential #-Message 'Please provide the username and password you want to store.'
$Filename = Verify-Path $Filename
$Credentials | Export-CliXml $Filename
Write-Host -NoNewLine -ForegroundColor "Yellow" "Credentials have been exported to: "
Write-Host -ForegroundColor "Green" "$($Filename)"
Write-Host -ForegroundColor "Yellow" "Please note that only $($env:username) can decrypt and use the credentials stored in this file."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment