Created
December 15, 2015 06:03
-
-
Save magnetikonline/a6db9c3835a65685df86 to your computer and use it in GitHub Desktop.
Creating a PowerShell PSCredential object with username/password using secure/encrypted strings.
This file contains hidden or 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
Set-StrictMode -Version Latest | |
$PASSWORD = "mypassword" | |
# create secure string from plain-text string | |
$secureString = ConvertTo-SecureString -AsPlainText -Force -String $PASSWORD | |
Write-Host "Secure string:",$secureString | |
Write-Host | |
# convert secure string to encrypted string (for safe-ish storage to config/file/etc.) | |
$encryptedString = ConvertFrom-SecureString -SecureString $secureString | |
Write-Host "Encrypted string:",$encryptedString | |
Write-Host | |
# convert encrypted string back to secure string | |
$secureString = ConvertTo-SecureString -String $encryptedString | |
Write-Host "Secure string:",$secureString | |
Write-Host | |
# use secure string to create credential object | |
$credential = New-Object ` | |
-TypeName System.Management.Automation.PSCredential ` | |
-ArgumentList "myusername",$secureString | |
Write-Host "Credential:",$credential |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment