Home > Blogs > VMware PowerCLI Blog > Monthly Archives: May 2008

Monthly Archives: May 2008

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.

Automation and TechEd 2008

If you’re like me you get bored with clicking through Wizard interfaces after about the 5th time or so. On the other hand scripting is a lot more powerful, but at the same time requires you to learn programming languages and APIs, not exactly a lot of fun. Whenever I’m staring at a 12-step wizard I always wish there was a way I could convert it to a spreadsheet input view that would let me fill in a bunch of them at once and then automatically run through everything for me. That’s the motivation for this spreadsheet . Basically it lets you enter an entire table of values, but takes care of all the scripting for you. Using the spreadsheet is very simple, but as always you will need to havei nstalled the VI Toolkit (for Windows) on your computer.

Here’s a video of me putting it to the test:


Creating lots of VMs, best viewed fullscreen.

The script is actually pretty simple, if you want to see it for yourself it’s hidden in cell A2 of the spreadsheet, or you can see it here.

foreach ($f in (import-csv "parameters.csv")) {
if ($f."Host Name") {
  $vmhost = get-vmhost $f."Host Name"
} else {
  $vmhost = get-vmhost
}
if ($f."Resource Pool") {
  new-vm -host $vmhost -name $f.Name -diskmb ([long]$f."Disk Size (GB)" * 1024) -memorymb \
   $f.Memory -networkname $f."Network Name" -resourcepool \
   (get-resourcepool $f."Resource Pool") -runasync
} else {
  new-vm -host $vmhost -name $f.Name -diskmb ([long]$f."Disk Size (GB)" * 1024) -memorymb \
   $f.Memory -networkname $f."Network Name" -runasync
}
}

Not bad for 15 lines of PowerShell.

If you’re feeling a bit adventurous you could easily modify the script to accept different values, or to perform different actions.

We’ll have this demo and many others, including cloning VMs and creating virtual switches, available for you to play with in real-time at our booth at TechEd. And stay tuned, I’m going to be giving a few more previews of the PowerShell power tools we’re going to have to play around. Hope to see you all there.

Ever wonder what this toolkit thing can do for you?

Brandon Shell has posted a terriffic video tour of the VI Toolkit Beta. If you’ve ever wondered what the toolkit can do for you, here’s your chance to get a very clear picture of how PowerShell can help you automate day-to-day administration.

Update: looks like Brandon is producing a series of videos. In this second video he shows how the toolkit can be used to control VM power state. Way to go Brandon!

Updated update: Brandon keeps churning out videos and now has a VMotion video. I can personally assure you that this video is better than anything in the theatres today, so save yourself 20 bucks and watch Brandon’s VMotion video!

And if you’d like to try Brandon’s scripts for yourself, don’t forget to download the VI Toolkit (for Windows) Beta.