Skip to main content

Reconnecting ESXi servers with PowerCLI

We recently needed to re-ip our vCenter servers; each with ~200 ESXi servers which needed to be reconnected - thank goodness for PowerCLI.  Since there isn't a "Reconnect-VMHost" cmdlet provided by PowerCLI we needed to check the HostSystem Object at https://www.vmware.com/support/developer/converter-sdk/conv55_apireference/vim.HostSystem.html and to see what methods were available (hint: there's a ReconnectHost_Task method which will do the trick).  We can still leverage the "Get-VMHost" cmdlet to return the disconnected ESXi servers and then call the ReconnectHost_Task method to reconnect each ESXi server.  The code is fairly short:

Get-VMHost -state Disconnected | foreach-object {
  $vmhost = $_
  $connectSpec = New-Object VMware.Vim.HostConnectSpec
  $connectSpec.force = $true
  $connectSpec.hostName = $vmhost.name
  $connectSpec.userName = 'root'
  $connectSpec.password = 'MySecretPassword'
  $vmhost.extensionData.ReconnectHost_Task($connectSpec,$null)
}


Since the ReconnectHost_Task returns immediately and doesn't wait for the task to complete then you will see multiple reconnect tasks queued in vCenter.  This method does return a "Task" object which you can monitor for completion (blog post coming soon on that) before spawning another reconnect task.

Comments

  1. # No need to repeat the following in the loop:
    $connectSpec = New-Object VMware.Vim.HostConnectSpec
    $connectSpec.force = $true
    $connectSpec.userName = 'root'
    $connectSpec.password = 'MySecretPassword'

    # Loop only needs to update hostname and invoke reconnect
    Get-VMHost -state Disconnected | foreach-object {
    $vmhost = $_
    $connectSpec.hostName = $vmhost.name
    $vmhost.extensionData.ReconnectHost_Task($connectSpec,$null)
    }

    ReplyDelete
  2. when running above script getting below error
    You cannot call a method on a null-valued expression.
    At C:\Users\root\Documents\myrec.ps1:25 char:42
    + $vmhost.extensionData.ReconnectHost_Task <<<< ($connectSpec)
    + CategoryInfo : InvalidOperation: (ReconnectHost_Task:String) []
    , RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

    ReplyDelete
  3. It worked for me. Thanks for the help.

    ReplyDelete
    Replies
    1. Does it help connect the hosts back to respective clusters?

      Delete

Post a Comment

Popular posts from this blog

Querying for nested folders with PowerCLI

Have you fought trying to query nested, duplicate-named folders?  Hopefully this will help solve the problem!  Suppose you have a VM folder-tree similar to this:   So, how do you get the "\dotcom\linux\dev" folder using PowerCLI?  If you query for just "dev" then you can get an array of folders.    You can parse through the array and, using the parent object, traverse the tree backwards validating the folder names.  But, what if you have 100s of folders?  In my opinion, this is not an optimal approach.   We really need to do this: This is great case for  recursion .  In my words, recursion is a "stack" of operations.  When an operation completes its result is used by the next operation in the "stack".  Most importantly there has to be base-case which causes the last operation in the stack to return a valid result.  Then each operation can be popped off the "stack" and its result can be used by...

Unmounting VMFS Datastores using vRO

Being a diligent admin, I always unmount a datastore from an ESXi server prior to completely deleting it.  It's a good way to verify the datastore isn't being used before the storage admin removes the ESXi server's visibility to the lun.  This is a task that I really want to perform in vRealize Orchestrator (vRO) instead of having to unmount the datastore from the ESXi server manually.   SPOILER ALERT - I'll include a workflow that allows you to execute the unmount across multiple ESXi servers in parallel at the end of this post. The first step is to see what's available from an unmount standpoint.  We start by bringing up the API Explorer under the Tools menu. From here we can search the "Attributes & methods" for text that contains the word unmount.  There quite a few methods which contain the text unmount.  I'm going to focus on the VcHostStorageSystem.unmountVmfsVolume() method.   This method takes a single argument - the uuid of ...