Powershell – parameters & credentials

Just a quick blog; am working on a script that requires credentials to run against a REST API, and a developer wanted to convert that script to use command-line parameters. I built this script (and quick test) to show that the command-line parameters create the same object as the Get-Credential object.

#define command line parameters
param (
    [string] $User,
    [string] $PWordClearText
)
#assign paramter values to appropriate objects, including creating a credential

$PWord = ConvertTo-SecureString -String $PWordClearText -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord

#Get credential from prompt
$Credential2 = Get-Credential

#compare the two credentials to validate
Compare-Object $Credential2.GetNetworkCredential().Password $Credential.GetNetworkCredential().Password -IncludeEqual

Nothing exciting or in-depth, but this is the foundation for automating a script I wrote to run under specified credentials.

Share