Powershell and the VI Toolkit for Windows

I’ve been using Perl for close to 10 years now, so my natural tendency has been to use Perl when creating automation scripts for virtual infrastructure.  Now for some reason, there has been a significant increase in the amount of questions I get about scripting repetitive virtual infrastructure tasks.  In fact, just yesterday a customer asked me “is there an easy way to rescan the HBA’s in all off my (many) hosts?”  With the GUI, this could translate into 100s of clicks and a very boring hour or more of work.  Of course, a properly written script could easily handle this, requiring a mere 30 seconds of his time.  And again, a few months ago I would have answered the question by pointing him towards Perl.

But recently there has been a lot of buzz around Powershell and the VI Toolkit for Windows.  So I started checking it out a few months ago and thus far, I’m very impressed.  Check this out, a script that would solve the HBA rescan problem I just described could be as simple as this …

Connect-VIServer –Server  ip_of_vc -User “username” -Password “password
Get-VMHost | Get-VMHostStorage –RescanAllHBA

Or, if you’d like the script to prompt you for the VirtualCenter IP, username and password, it would look like this …

$vc = Read-Host “Enter the IP address of your VirtualCenter server”
$un = Read-Host “Enter your username”
$pw = Read-Host “Enter your password”
Connect-VIServer –Server  $vc -User $un -Password $pw
Get-VMHost | Get-VMHostStorage –RescanAllHBA

Pretty easy, huh?  Here is a quick little script I wrote for a customer that’s standing up a large number of ESX servers and they didn’t want to manually add all of them to VirtualCenter.  The DNS names of their ESX servers were computernamexxx.domain.com.

## Gather info and connect to VC
$vc = Read-Host “Enter the IP address of your VirtualCenter server”
$un = Read-Host “Enter your username”
$pw = Read-Host “Enter your password”
Connect-VIServer -Server $vc -User $u -Password $p
Write-Host “Connected to VirtualCenter ($vc)”

## List available datacenters and select which one to add the ESX servers
$adc = Get-Datacenter
Write-Host $adc
$dc = Read-Host “Which Datacenter?”

## Gather the number of ESX hosts, the root password and the first ESX to be added
$nh = Read-Host “How many hosts are we connecting?”
$rp = Read-Host “What is the root password of the hosts?”
[int]$start = Read-Host “What is the number of the first host (computernamexxx.domain.com)?”
$stop = $start + $nh

## Add them to VC
while ($start -le $stop) {

$x = “{0:0##}” -f $start   ## <– leading zeros
$myhost = “computername” + “$x” + “domain.com”
Add-VMHost $myhost -Location (Get-Datacenter $dc) -User root -Password $rp
Write-Host “$myhost is now connected to VirtualCenter ($vc)”
$start++
}

Or, here’s a quick and dirty one liner I used to remove the VLAN ID’s on a port group.  If you want to add or change the VLAN ID, rather than remove it, simply change the 0 at the end of the line to the actual VLAN ID.

$hosts = Get-VMHost; foreach ($x in $hosts) {get-virtualportgroup -host (get-vmhost $x)  -name “portgroup” | set-virtualportgroup -vlanid 0}

As you can probably tell, I’ve decided that I like Powershell and the VI Toolkit for Windows.  It’s VERY handy.  In fact, I’m thinking of collecting my favority scripts, tools and links and compiling a separte page.  So look for that coming soon.  For now, here are some essentials:

Happy scripting! 🙂

One comment Add yours
  1. What I don't get is this… I can execute Powershell scripts from the Powershell prompt. But if I try to use any of the VI Toolkit functionality it errors out. I have to actually be at the “VI Toolkit” command prompt for scripts to run successfully. Did I miss an important integration piece that will marry the VI Toolkit environment to the Powershell environment?

Leave a Reply

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