PowerCLI: Reconnect VMhosts after changing vCenter certificates

If you have ever changed the vCenter server certificates, you’ve experienced having all your hosts disconnected from vCenter.  I couldn’t imagine reconnecting them one at a time… You could do this all natively in PowerCLI, but that would require you to fully remove and then add the hosts.  That is very inconvenient, and almost as much trouble as doing it by hand… In this case it is both faster and easier to just use the native vSphere API.

~Glenn

4 thoughts on “PowerCLI: Reconnect VMhosts after changing vCenter certificates”

  1. Alan,
    In this case no, as I understand it vCenter uses it’s private key to encrypt the password for each host. When you change vCenters server certificates it can no longer decrypt said password. If you look in the logs the hosts are disconnected because of a failed login (bad password). Therefor we have to update the host credentials… this is accomplished via the reconnectHost method.

    The pure PowerCLI method would be Add-VMHost, but that wont work unless we first Remove-VMHost. Sounds simple enough, but we would first have to account for cluster, resourcepools, vm folders, etc… as you can see it get’s messy. What we need is a connect-vmhost cmdlet that let you set the credentials.

    Reply
  2. Thanks for an excellent script!

    In API 5 the ReconnectHost_Task method requires 2 parameters instead of 1.

    # Get the hostsystem object for every host currently disconnected.
    $VMhosts = Get-View -ViewType ‘Hostsystem’ ` -Property ‘name’ ` -Filter @{“Runtime.ConnectionState”=”disconnected”}
    Foreach ($VMhost in $VMhosts) {
    # Create a reconnect spec
    $HostConnectSpec = New-Object VMware.Vim.HostConnectSpec
    $HostConnectSpec.hostName = $VMhost.name
    $HostConnectSpec.userName = ‘root’
    $HostConnectSpec.password = ‘password’
    # Reconnect the host
    $taskMoRef = $VMhost.ReconnectHost_Task($HostConnectSpec,$null)
    # optional, but i like to return a task object, that way I can
    # easily integrate this into a pipeline later if need be.
    Get-VIObjectByVIView -MORef $taskMoRef
    }

    Reply

Leave a Reply