VMware Cloud Community
a2alpha
Expert
Expert
Jump to solution

Script login to viserver with read-host for name username and password assecurestring

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

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Or use the script I entered in

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

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

LucD
Leadership
Leadership
Jump to solution

Or use the script I entered in

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

All though I would prefer Luc's first answer, you can fix your code as in the attached script.

Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
a2alpha
Expert
Expert
Jump to solution

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.

0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

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.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Thanks Luc, I will do that in the future. And I will change the posts I allready did.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
a2alpha
Expert
Expert
Jump to solution

Thanks Robert, I completely missed that as well. Appreciate your help.

Dan

0 Kudos