Last active
October 2, 2017 20:44
-
-
Save star-crossed/5b9a150de6a62102651e72e0a2b1068f to your computer and use it in GitHub Desktop.
A snippet for keeping a credential in Windows Credential Manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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