Hi,
I am scripting the login to a vCenter server or ESX host at the start of another script, to keep it generic I want to prompt the user for the server name, username and password and pass this to the connect-viserver. I would really like the password entry to be stared out and can see how to do it but if it is stored as a secure string, it doesn't pass into the connect-viserver command properly. If I take off the -assecurestring it connects fine.
$vc = read-host "Enter vCenter name or ESX host name"
$us = read-host "Enter Username"
$pw = read-host "Enter Password" -assecurestring
connect-viserver $vc -user $us -pass $pw
...
I have tried adding the line:
$pw = $pw | out-string
But it doesn't convert it.
How do you read a secure string back into the command?
Thanks,
Dan
Or use the script I entered in
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Why don't you do it like this
Connect-VIServer -Server <servername> -Credential (Get-Credential)
Now you will get a prompt for the user and password.
And the password is not visible while being typed.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Or use the script I entered in
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you both for this, I have decided to go with LucD from the other post, I need to spend a bit more time searching before asking the question!!
$vc = read-host "Enter vCenter name or ESX host name"
$us = read-host "Enter Username"
$pw = read-host "Enter Password" -assecurestring:$true
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $us,$pw
Connect-VIServer -Server $vc -Credential $cred
Robert, I would prefer to use something that doesn't require internet access as some of our customers use proxys but thanks.
I looked at the get-credential option but I would prefer all command line rather than pop out box for cleaness!
Thanks again.
I now see that the forum software broke my code. You don't need to access the internet. I just used some .NET classes. I don't know how to insert them in a reply without making the software change them into hyperlinks. If someone can explain that to me, that would be nice. I will edit my first reply so the hyperlinks are gone. But I need to insert a \ before the [. That is the only trick know.
Robert, when I have square brackets in my code, I attach the script as a file.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Luc, I will do that in the future. And I will change the posts I allready did.
Thanks Robert, I completely missed that as well. Appreciate your help.
Dan