Tuesday, June 5, 2018

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 ***

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)


Notes

If you change the machine or account that this runs as, you will have to recreate this file!


Reference

https://practical365.com/blog/saving-credentials-for-office-365-powershell-scripts-and-scheduled-tasks/

No comments: