$PasswordFile = "\\xxx.xx\xx\xx\xx\xx xx\xx.txt"
$KeyFile = "\\xx.com\xx\xx\xx\xx xx\xx.key"
[Byte[]] $key = (1..16)
$vPassword = "xxxxx" | ConvertTo-SecureString -AsPlainText -Force
$vPassword | ConvertFrom-SecureString -key $key | Out-File $PasswordFile
[Byte[]] $key = (1..16)
Get-Content $PasswordFile | ConvertTo-SecureString -Key $key
After above i do below and i get error :
Connect-VIServer -Server xx.xx.x.xx -User xxx -Password $vPassword
Error:
Cannot complete login due to an incorrect user name or password.
This is a 3-step
Something like this
# File locations
$keyFile = 'C:\Temp\aes.key'
$pswdFile = 'C:\Temp\pswd.txt'
# Step 1 - Create key file
$key = New-Object Byte[] 32
[Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($key)
$key | Out-File -FilePath $keyFile
# Step 2 - Create password file with key encryption
$user = 'user'
$pswd = 'VMware1!'
$secPswd = $pswd | ConvertTo-SecureString -AsPlainText -Force
$secPswd | ConvertFrom-SecureString -Key (Get-Content -Path $keyFile) |
Set-Content -Path $pswdFile
# Step 3 - Retrieve password
$encryptedPswd = Get-Content -Path $pswdFile | ConvertTo-SecureString -Key (Get-Content -Path $keyFile)
$cred = New-Object System.Management.Automation.PSCredential($user,$encryptedPswd)
# Step 4 - Use credential
Connect-VIServer -Server vc -Credential $cred
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Get-VICredentialStoreItem | This cmdlet retrieves the credential store items available on a vCenter Server system. |
New-VICredentialStoreItem | This cmdlet creates a new entry in the credential store. |
Remove-VICredentialStoreItem | This cmdlet removes the specified credential store items. |
Examples
C:\PS>New-VICredentialStoreItem -Host vCenter01 -User Admin -Password pass
C:\PS>Remove-VICredentialStoreItem -Host vCenter01 -User Admin
see link - Back to Basics: Connecting to vCenter or a vSphere Host - VMware PowerCLI Blog - VMware Blogs