VMware Cloud Community
wallakyl
Enthusiast
Enthusiast
Jump to solution

Script to reconnect hosts using 'Read-Host -AsSecureString'

I am trying to get a script which will reconnect all of the ESX hosts when using a different vCenter server (same database though). I found the basics of what I need here:

PowerCLI Blog

But the only problem with that one is that the script has the password in clear text. I would prefer to prompt for the password, as with the Read-Host -AsSecureString option. My script below works fine without the -AsSecureString option....when the Read-Host is not masked. Can anyone tell me how to get it working with the -AsSecureString?

$password = Read-Host -assecurestring "Please enter the root password"

Get-VMHost | % { $view = Get-View $_.id }

$arg = New-Object vmware.vim.hostconnectspec

$arg.userName = "root"

$arg.password = $password

$arg.force = $true

$view.ReconnectHost($arg)

Thanks!

Kyle

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The password property in the HostConnectSpec object requires a clear-text password.

That means you would have to use the method Robert provided in that post.

I attached a file (since the forum SW doesn't like square brackets) with your script adapted for that method.

____________

Blog: LucD notes

Twitter: lucd22


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

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

There just has been another post that addressed the same problem.

Have a look at

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
wallakyl
Enthusiast
Enthusiast
Jump to solution

Thanks LucD.

I had seen that, but it hasn't worked for me. I get the same error I did before. I think my situation is slightly different because I am passing the password into an argument for the reconnect host? I am not sure - I am still pretty new to PowerCLI.

Here's what I've got with your $cred variable.

Clear-Host
$vc= Read-Host "Please enter the FQDN of the vCenter server that is being activated"
Connect-VIServer $vc
Clear-Host
$passwd = Read-Host -AsSecureString "Please enter the root password" 
Get-VMHost | % { $view = Get-View $_.id }
	$arg = New-Object vmware.vim.hostconnectspec
	$arg.username = "root"
	$arg.force = $true
	$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $arg.userName,$passwd
	$arg.password = $cred
$view.ReconnectHost($arg)

And here is the error, which is the same error I get without the $cred variable (screenshot).

Thanks for your help!

Kyle

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The password property in the HostConnectSpec object requires a clear-text password.

That means you would have to use the method Robert provided in that post.

I attached a file (since the forum SW doesn't like square brackets) with your script adapted for that method.

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
wallakyl
Enthusiast
Enthusiast
Jump to solution

Awesome, exactly what I was looking for. Thanks for your help!!

Reply
0 Kudos