Below is the code I have to get data from a linux VM.
$username = Read-Host "Please enter VM Admin user Name"
$password = Read-Host -assecurestring "Please enter VM Admin password"
$password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
$plinkpath = 'C:\Program Files\PuTTY\'
$servername = Read-Host "Please enter targeted linux VM IP Address"
$cmd = 'df -h'
$diskinfo = @(echo y | &($plinkpath "plink.exe") -pw $password $username@$servername $cmd)
If I pass the password like this I am not able to get the output of the command, but when I send password as raw string
$password = Read-Host "Please enter VM Admin password"
I am able to get the output of the command.
Please let me know what is the reason for this.
CodePudding user response:
The reason is -assecurestring
which should show *
instead of typed output.
For more see Read-Host MS manual .
For better readability:
$password = Read-Host "Please enter VM Admin password" -AsSecureString