Using Encrypted Credentials in PowerShell
*** NOTE: This must be done when logged into the computer running the program, as the user you’ll be running the program as. E.g. if your Scheduled Task is running as sys_admin, you must be logged in as sys_admin on the machine that will be running the scheduled task before encrypting the credentials ***
$cred = Get-Credential
$cred.Password | ConvertFrom-SecureString | Out-File .\adcreds.txt
$User = "sregan@vivityconsulting.com"
$File = "C:\Development\Vivity\adcreds.txt"
$cred = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)
Create the Encrypted File
First create the encrypted text file by running the following:$cred = Get-Credential
$cred.Password | ConvertFrom-SecureString | Out-File .\adcreds.txt
Using the Encrypted Credentials
To use the credential file in powershell you can use:$User = "sregan@vivityconsulting.com"
$File = "C:\Development\Vivity\adcreds.txt"
$cred = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)
Comments