Skip to content

Instantly share code, notes, and snippets.

@shuguet
Last active April 27, 2020 16:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shuguet/6cb4577414357c25dbeb3d018a4b7373 to your computer and use it in GitHub Desktop.
Save shuguet/6cb4577414357c25dbeb3d018a4b7373 to your computer and use it in GitHub Desktop.
How to use encrypted password in NTNX PowerShell script
#Creating AES key with random data and export to file:
$KeyFile = "c:\AES.key"
$Key = New-Object Byte[] 16 # You can use 16, 24, or 32 for AES
[Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($Key)
$Key | out-file $KeyFile
#Creating SecureString object:
$PasswordFile = "c:\Password.txt"
$KeyFile = "c:\AES.key"
$Key = Get-Content $KeyFile
$Password = "nutanix/4u" | ConvertTo-SecureString -AsPlainText -Force
$Password | ConvertFrom-SecureString -key $Key | Out-File $PasswordFile
# Define a few variables
$User = "admin"
$NTNXCluster = "my.nutanix.cluster.local.ip"
$PasswordFile = "c:\Password.txt"
$KeyFile = "c:\AES.key"
#Creating PSCredential object
$key = Get-Content $KeyFile
$MyCredential = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $User, (Get-Content $PasswordFile | ConvertTo-SecureString -Key $key)
Connect-NTNXCluster -Server $NTNXCluster -UserName $MyCredential.UserName -Password $MyCredential.Password -AcceptInvalidSSLCerts -ForcedConnection
Get-NTNXVM | select VMName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment