Managing Credentials (Part 2)

by Jan 5, 2017

For scripts running unattended, you can create login credentials from code. This requires the secret password to be saved as clear text in a script (which obviously is unsafe unless you encrypt your script with the encrypting file system (EFS) or take other measures to protect the content):

$password = 'topsecret!' | ConvertTo-SecureString -AsPlainText -Force
$username = 'myCompany\myUserName'
$credential = New-Object -TypeName PSCredential($username, $password) 

# use the credential with any cmdlet that exposes the –Credential parameter
# to log in to remote systems
Get-WmiObject -Class Win32_LogicalDisk -ComputerName SomeServer -Credential $credential

Twitter This Tip! ReTweet this Tip!