Reconnect ESXi host after reinstall

After a VMware ESXi host reinstall reconnecting your host is not as easy as changing the state of the host by using the Set-VMHost -State command in VMware PowerCLI. The reason is that your host has gotten a new certificate. So to do this scripted you need to retrieve the SSL thumbprint and specify that during the reconnect.

This script will help you do all that. I need to give credit to William Lam (@lamw), who I borrowed the SSL part from. You can find his original post here: https://gist.github.com/lamw/988e4599c0f88d9fc25c9f2af8b72c92

Function Get-SSLThumbprint {
    param(
    [Parameter(
        Position=0,
        Mandatory=$true,
        ValueFromPipeline=$true,
        ValueFromPipelineByPropertyName=$true)
    ]
    [Alias('FullName')]
    [String]$URL
    )

add-type @"
        using System.Net;
        using System.Security.Cryptography.X509Certificates;
            public class IDontCarePolicy : ICertificatePolicy {
            public IDontCarePolicy() {}
            public bool CheckValidationResult(
                ServicePoint sPoint, X509Certificate cert,
                WebRequest wRequest, int certProb) {
                return true;
            }
        }
"@
    [System.Net.ServicePointManager]::CertificatePolicy = new-object IDontCarePolicy

    # Need to connect using simple GET operation for this to work
    Invoke-RestMethod -Uri $URL -Method Get | Out-Null

    $ENDPOINT_REQUEST = [System.Net.Webrequest]::Create("$URL")
    $SSL_THUMBPRINT = $ENDPOINT_REQUEST.ServicePoint.Certificate.GetCertHashString()

    return $SSL_THUMBPRINT -replace '(..(?!$))','$1:'
    # Credit - https://gist.github.com/lamw/988e4599c0f88d9fc25c9f2af8b72c92
}

Function Reconnect-VMHost {
<#

Requires -Modules VMware.PowerCLI

.SYNOPSIS
    Used to reconnect a VMHost after a reinstall.

.DESCRIPTION
    This function will help you reconnect a host to vCenter, after it
    has been reinstalled.

    I will take care of the authentication and ssl thumbprint change.

.PARAMETER VMHost
    You need to provide a VMHost object.

.PARAMETER Username
    You need to provide a username.
    
.PARAMETER Password
    You need to provide a password.

        
.EXAMPLE
    C:\PS>
    $VMHost = Get-VMHost hostname.domainname.com
    Reconnect-VMHost -VMHost $VMHost -Username 'root' -Passord 'password'

    or

    $vmhost | Reconnect-VMHost -Username 'root' -Password 'password
    
.NOTES
    Author: Brian F. Knutsson - CRIT Solutions
    Date:   October 27, 2021


#>
[cmdletbinding()]
param(
    [parameter(
        Position=0,
        Mandatory=$true,
        ValueFromPipeline=$true)
    ]
    [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl]$VMHost,
    [string]$Username,
    [string]$Password
)

    try {
        if ($VMHost.GetType().name -ne 'VMHostImpl') {
            $errorCode = "VMHost not defined"
            throw($errorCode)
        }
    }

    catch {
        $errorCode = "VMHost not defined"
        throw($errorCode)
    }

    #Write-Host("Host: " + $VMHost)
    #Write-Host("Username: " + $Username)
    #Write-Host("Password: " + $Password)

    # vCenter Server URL
    $vcurl = "https://" + $VMHost

    # Example output

    $thumbprint = Get-SSLThumbprint $vcurl

    Write-Host("Thumbrint: " + $thumbprint)

    $VMHostView = $VMHost | Get-View  -Property 'name'

    # Create a reconnect spec
    $HostConnectSpec = New-Object VMware.Vim.HostConnectSpec
    $HostConnectSpec.hostName = $VMHostView.name
    $HostConnectSpec.userName = $Username
    $HostConnectSpec.password = $Password
    $HostConnectSpec.SslThumbprint = $thumbprint

    # Reconnect the host
    $taskMoRef = $VMHostView.ReconnectHost_Task($HostConnectSpec,$null)
}

Hope this helps you. Keep in mind that use of this script is entirely your responsibility, and you should always understand what a script does before you run it.

Related Post