Sometimes you need a credential object in scripts to run that script unattended. This article describes a way to create a credential object to use it in interactive and non-interactive scripts.

Common Scripts

Interactive-Mode

You can create the PSCredential object by using Get-Credential cmdlet which opens a dialog to enter the username and password. This way of entering credentials can be used in an interactive mode.

$Credential = Get-Credential

Non-Interactive-Mode

When you have to provide credentials in a script, to use a scheduled task, you can create a PSCredential object in the following way.

$Passwd = ConvertTo-SecureString "PlainTextPassword" -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential ('username', $Passwd)

You can now pass $Credential to any -PSCredential input parameter

Azure Automation

In Azure, you can create an Azure Automation account and then you can run Runbooks defined by a Job. If you need to log in to a resource in a runbook, you can create a Credential object at the automation account.

This account can be used in runbooks like this.

# Build Authentication object
$Credential = Get-AutomationPSCredential -Name 'Username'

Have fun

Leave a Reply

Your email address will not be published. Required fields are marked *