VMware Cloud Community
thiag2011
Enthusiast
Enthusiast

Script to reconnect multiple ESXi host to vcenter using a specific username and password

Hi all,

Am in need of a script which reconnects ESXi hosts that are in disconnected state from vcenter.

Could someone help me please.

Reconnecting ESXi hosts manually entering user name and pwd is becoming a tedious task after SSL certificate renewal.

0 Kudos
8 Replies
thiag2011
Enthusiast
Enthusiast

Hi all,

Below script can connect hosts back to vcenter.

$Hosts = Get-VMHost | where { $_.ConnectionState -eq "Disconnected" }

foreach ($host in $Hosts) {

  Set-VMHost -VMHost $host -State Connected

}



Issue is that, am not aware of how to supply username and password to it.

Please help.


LucD

Could you please help me on this.

Its bit urgent.

0 Kudos
LucD
Leadership
Leadership

Shouldn't you be using the Add-VMHost cmdlet for this ?

On there you specify the credentials.


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

0 Kudos
thiag2011
Enthusiast
Enthusiast

Hi LucD

No, actually the purpose of this script is to readd the disconnected host to its same location.

In a vcenter we have more that 300 hosts in 25 datacenters, in different folders.

So am not sure whether add-vmhost will help in this case as i have to give the parameter "Location", where the location folder above the cluster will be the same but only the datacenter name will differ..

Any other way please?

0 Kudos
LucD
Leadership
Leadership

I suspect we are suffering from different interpretation of terminology here.

The Set-VMHost cmdlet is used to change the state of a VMHost that is already connected to your vCenter.

For example to change the state from "maintenance" to "connected".

The Add-VMHost cmdlet is used to "add" a new VMHost to the vCenter.

Since you are prompted for credentials when doing a Set-VMHost, it seems to indicate that the ESXi node is not known to the vCenter anymore.

Or that you changed the root credentials of the ESXi node.

What exactly is returned when you do a Get-VMHost for one of these disconnected ESXi nodes ?


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

0 Kudos
Prakas
Enthusiast
Enthusiast

Use a different variable instead of '$host' as it is a system defined constant.

My guess is Set-VMHost should connect the host to vCenter without prompting for credentials.

0 Kudos
thiag2011
Enthusiast
Enthusiast

Hi LucD,

After renewing the SSL certificate, sometimes, the esxi hosts gets disconnected from vcenter.

In this case, I have to reenter my username and password for all the hosts when i connect manually.

Instead of this, if am able to use set-vmhost and provide my credentials , then it reconnects the disconnected hosts to vcenter.

0 Kudos
thiag2011
Enthusiast
Enthusiast

Hi Prakash,

Yes the variable $host shouldnt be used.

It is not connecting by itself, after my SSL certificate renewal, but reconnects, if i provide the password and username manually while readding the host to vcenter..

0 Kudos
thecloudxpert
Enthusiast
Enthusiast

Try using the following

You could create a CSV file with vCenter, vDCName, ClusterName, ESXHost, Username, Password.  Then import that into a PowerCLI script and run through the list with the following commands.

I have a very similar script for my small home lab but as its only two hosts I have just an array within the script

The create an encrypted password object using $EncryptedPassword = ConvertTo-SecureString -String "$Password" -AsPlainText -Force


A PowerShell Credential Object using $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username, $EncryptedPassword

Then use Get-DataCenter -Name $VDCName | Get-Cluster -Name $ClusterName | Add-VMHost -Name "$ESXHost" -Credential $Credential -Force -ErrorAction SilentlyContinue

HTH

0 Kudos