Home > Blogs > VMware PowerCLI Blog


Adding and re-adding ESX servers to VirtualCenter

Hosts can get disconnected for a variety of reasons. One common reason is that the host was imported into another instance of VirtualCenter, since only one VirtualCenter can manage an ESX host at a time.

Here’s what a host (in my case, 10.16.83.242) looks like when it’s disconnected:

Disconnected

In other words a disconnected host appears in inventory but is unmanagable. If you have a lot of disconnected hosts, here’s a PowerShell script you can use to automatically reconnect every host (new and improved with real-world customer feedback! Thanks Tom!):

# Reconnect all hosts in VirtualCenter. Connect first using Get-VIServer.
# If you need a password, set it here.
$password = $null
get-vmhost | where { $_.State -eq "Disconnected" -or $_.State -eq "notResponding" } |
% {
$view = get-view $_.ID
$arg = new-object VMware.Vim.HostConnectSpec
$arg.userName = "root"
$arg.password = $password
$arg.force = $true
if ($_.State -eq "notResponding") {
$return = $view.DisconnectHost_Task()
wait-task $return
}
$view.ReconnectHost_Task($arg)
}

This script automatically detects any disconnected host and reconnects it (be sure you connect to VirtualCenter before running it though!) On the first line, the script also allows setting a password Sometimes you can’t reconnect a host without a password, usually because the password has been changed. If this applies to you, set the password here and you’ll be re-connecting hosts faster than ever.

On the other hand…

If an ESX host you want to manage doesn’t appear in VirtualCenter, you need to add it. This is a bit tricker than reconnecting since there’s no inventory in VirtualCenter to tell you the IP addresses of all the hosts you use, and you also need to know a host’s password in order to add it. This is another case where entering things in a spreadsheet can really speed things up. I made just such a spreadsheet, and here’s a video of me using it.

Attaching lots of VMware ESX hosts to VMware VirtualCenter.

One big improvement this time around is that we enter the passwords at runtime rather than saving them in the spreadsheet.

As always with these spreadsheets, the script that makes it all work is hidden in cell A2, so if this is not quite what you need you can easily tweak it into something more like what you need.

Also, if you haven’t yet downloaded the VI Toolkit (for Windows), there’s no reason not to give it a try today.

5 thoughts on “Adding and re-adding ESX servers to VirtualCenter

  1. James Brundage

    Quick PowerShell best practice:
    Instead of leaving usernames and passwords within a script, you can actually use the built in PowerShell Cmdlet: Get-Credential

    Reply
  2. shankyrhodes

    Hi,
    we are considering to buy a VMware Virtual Center.
    We have two servers running VMware Standard edition.
    Do you believe it will be worth it? Or do we have to
    upgrade our VMware licenses to Enterprise before upgrading
    virtual center to make it worth it. I had just read the
    following article
    VMware virtual center real value

    Reply
  3. Jamie

    I get this every time it try to run this script
    Please help
    The term ‘get-viserver’ is not recognized as the name of a cmdlet, function, sc
    ript file, or operable program. Check the spelling of the name, or if a path wa
    s included, verify that the path is correct and try again.
    At C:\Documents and Settings\jamie.smith\My Documents\script.ps1:17 char:13
    + get-viserver <<<< -server $server -credentials $credentials
    + CategoryInfo : ObjectNotFound: (get-viserver:String) [], Comman
    dNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>