Secure credentials in a Powershell script

When you are writing a script using powershell and need to send specific credentials in a connection is not a good practice write users and passwords in an easy readable file.

A good practice is to store this in a file with encrypted password

1.- Create file

$Credential = Get-Credential
$Credential | Export-CliXml -Path "c:\temp\Cred.Cred"

2.- Invoke credentials in script

$cred = Import-Clixml -Path C:\temp\Cred.Cred

User invoked credentials in your connection

If cmdlet accept -Credential parameter

$Connect-VIServer -Server $Host -Credential $Credential

If cmdlet don’t accept -credential parameter and you need a -Username and -Password you can use in this way

Connect-VIServer -Server $Host -User $credenciales.UserName -Password $credencial.GetNetworkCredential().password

Hope this helps

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.