Wednesday, 3 April 2013

Orchestrator & VMware vSphere – Calculate Datastore Capacity

While there is an official VMware vSphere Integration Pack for Orchestrator available from Microsoft (here) I was working for a customer the other week where I ran into a problem that meant we couldn’t use it and I had to work around it.

The problem looks to stem from having a vApp present within their vSphere environment, as discussed in this TechNet forum thread: http://social.technet.microsoft.com/Forums/en-US/scoscip/thread/2a1c03db-c24b-43c8-b035-f7c8cd6f6a83/

The biggest problem we had is that the Get Datastore Capacity activity which was failing was the very first step of the process that we were trying to automate.

The customer wanted to be able to check that there was sufficient space as dictated by their SoP (Standard Operating Proceedure) on the Datastore before taking snapshots of the VM’s.
So the quickest way to achieve this, fall back to PowerShell and script it.

VMware have a PowerShell module available for vSphere known as PowerCLI.
Once I had downloaded and installed this on both the Runbook Designer workstations and the SCORCH server I started working on the script.

N.B. There is also a community IP from Ryan Andorfer that wraps the PowerCLI into activities, but it's based on PowerCLI v4 and isn't compatible with PowerCLI v5 which is what I wanted to utilise.

This is a modified version of the script that LucD discusses on the VMware community forums here: http://communities.vmware.com/message/2119043

The script:

$VC = "<Enter vCenter name here>"

if(-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue))


{
 
Add-PSSnapin VMware.VimAutomation.Core 

}
 
Set-PowerCLIConfiguration -DefaultVIServerMode Single -InvalidCertificateAction Ignore -confirm:$false

Connect-VIServer -Server $VC -ErrorAction SilentlyContinue

$VMInfo=Get-VM "<Enter VM Name here>" | Select Name, MemoryGB, UsedSpaceGB, @{N="Cluster";E={Get-Cluster -VM $_}}, @{N="ESX Host";E={Get-VMHost -VM $_}}, @{N="Datastore";E={Get-Datastore -VM $_}}

$ESXiHost=$VMInfo.ESXHost.name

$DSName=$VMInfo.datastore.name

$VMMem=$VMInfo.MemoryGB

$VMHD=$VMInfo.UsedSpaceGB

$Datastore=Get-Datastore -Name $DSName | select Name, @{N="Capacity";E={[Math]::Round($_.CapacityMB/1024,2)}}, @{N="FreeSpace";E={[Math]::Round($_.FreeSpaceMB/1024,2)}}

$Capacity=$Datastore.Capacity

$FreeSpace=$Datastore.FreeSpace

*Sorry about the formatting, blogger mangles code*

Basically this script will take the input of a virtual machine name, lookup which datastore the VM resides on and then query that datastore for the available free space and output it as a variable that we capture onto the SCORCH databus.
While I was writing the script and finding the datastore for the VM I thought we might as well pull back some other info regarding the VM and ESX host so the script also makes some more information available as variables that we can push back onto the databus.

Variables:
Datastore Name ($Datastore)
Datastore Capacity ($Capacity)
Datastore FreeSpace ($FreeSpace)
VM Assigned Memory ($VMMem)
VM Assigned Disk Size ($VMHD)
ESX Host running the VM ($ESXiHost)

Add this into a simple runbook as shown below and we can prompt for a virtual machine name (or pass it across from a calling runbook!), query vSphere for all the details and then return the data to be used as part of the further process.


No comments: