Skip to content

Instantly share code, notes, and snippets.

@star-crossed
Last active October 2, 2017 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save star-crossed/5b9a150de6a62102651e72e0a2b1068f to your computer and use it in GitHub Desktop.
Save star-crossed/5b9a150de6a62102651e72e0a2b1068f to your computer and use it in GitHub Desktop.
A snippet for keeping a credential in Windows Credential Manager
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false)][Boolean]$setCredentials
)
Set-Strictmode -Version 1
Import-Module CredentialManager
If ($PSCommandPath -eq $null) {
Function GetPSCommandPath() {
return $MyInvocation.PSCommandPath;
}
$PSCommandPath = GetPSCommandPath;
}
$target = Split-Path $PSCommandPath -Leaf
$cred = Get-StoredCredential -Target $target
If ($cred -eq $null -or $setCredentials -eq $true) {
$cred = Get-Credential
$storedCred = New-StoredCredential -Target $target -Credential $cred -Persist LocalMachine
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment