Wednesday, June 4, 2008

VMware SDK with Powershell

This post provides a script using the VMware SDK with PowerShell to access a VI3 VirtualCenter server and enumerate the VMs. It's really just a proof of concept of what's possible, and a simple example of how to use the rather convoluted (in my opinion) VMware SDK.

Note that VMware have a PowerShell snap-in that makes everything VMware-related *much* easier, but there will always be lower-level tasks that the SDK is useful for.


# VMware SDK automation.
#
# Changes:
#  30/01/2008, Wayne Martin, Initial version
# 
# References: 
#  http://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.vm.GuestInfo.NicInfo.html
#
# Note that you need the compiled VMware SDK VimService.dll for this to work, and I'm using http rather than SSL - you may have to modify your vpxd.cfg 

$vmPath = ".\VimService.dll"
[Reflection.Assembly]::LoadFrom("$vmPath")

$serviceURL = "http://VCServer/sdk"
$usernameSDK = "domain\user"
$passwordSDK = "password"
$localeSDK = "en_au"

$pathDatacenter = "/DATACENTER"
$pathVMFolder = $pathDatacenter + "/vm"

$svcRef = new-object ManagedObjectReference
$svcRef.type = "ServiceInstance"
$svcRef.Value = "ServiceInstance"

$service = new-object VimService
$service.Url = $serviceURL
$service.CookieContainer = new-object System.Net.CookieContainer

$svcContent = $service.RetrieveServiceContent($svcRef)
$propCollectorRef = $svcContent.PropertyCollector

$service.Login($svcContent.sessionManager, $usernameSDK, $passwordSDK, $localeSDK)
$rootFolder = $svcContent.rootFolder

$dc2f = new-object TraversalSpec
$dc2f.type = "Datacenter"
$dc2f.path = "vmFolder"
$dc2f.skip = $false
$dc2f.selectSet = new-object SelectionSpec
$dc2f.selectSet[0].name = "traverseChild"
 
$traversalSpec = new-object TraversalSpec
$traversalSpec.type = "Folder"
$traversalSpec.name = "traverseChild"
$traversalSpec.path = "childEntity"
$traversalSpec.skip = $false
$traversalSpec.selectSet = new-object SelectionSpec
 
$sspecElement = new-object SelectionSpec
$traversalSpec.selectSet = $sspecElement, $dc2f
$traversalSpec.selectSet[0].name = $traversalSpec.name

$pfSpec = new-object PropertyFilterSpec
$pfSpec.propSet = new-object PropertySpec
$pfSpec.propSet[0].type = "VirtualMachine"
$pfSpec.propSet[0].all = $false
$pfSpec.propSet[0].pathSet = "config.name", "config.uuid", "config.extraConfig", "config.hardware", "guest.net"
#$pfSpec.propSet[0].pathSet = "config.name", "guest.net"

$pfSpec.objectSet = new-object ObjectSpec
$pfSpec.objectSet[0].obj = $rootFolder
$pfSpec.objectSet[0].skip = $false
$pfSpec.objectSet[0].selectSet = new-object SelectionSpec
$pfSpec.objectSet[0].selectSet[0] = $traversalSpec

$pfSpecRes = new-object PropertyFilterSpec 
$pfSpecRes = $pfSpec

$retProp = $service.retrieveProperties($propCollectorRef, $pfSpecRes)

for ($i= 0; $i - $retProp.Length; $i += 1) {
 $ObjContent = $retProp[$i]
 $propSet = $ObjContent.propSet
 for ($y= 0; $y - $propSet.Length; $y += 1) {
  $dynProp = $propSet[$y]
  if (!$dynProp.val.getType().IsArray) {
   write-host $dynProp.Name ": " $dynProp.val
  } else {
   for ($z= 0; $z - $dynProp.Val.Length; $z += 1) {
    $OCdynProp = $dynProp.val[$z]
    If ($OCdynProp.getType().ToString() -eq "GuestNicInfo") {
     write-host "MAC:" $OCdynProp.get_macAddress()
     write-host "IP:" $OCdynProp.get_IPAddress()
     write-host "Network:" $OCdynProp.get_network()
    } else {
     write-host $OCdynProp.get_key() ": " $OCdynProp.get_value()
    }
   }
  }
 }
 write-host
} 


#for ($i= 0; $i - $retProp.Length; $i += 1) {
# $ObjContent = $retProp[$i]
# $propSet = $ObjContent.propSet
# write-host $propSet[0].val "-" $propSet[1].val[0].macAddress "-" $propSet[1].val[0].IPAddress
#
#}
#return
#get-member -i $vmFolderMOR | write-host



Wayne's World of IT (WWoIT), Copyright 2008 Wayne Martin.

1 comment:

Hal said...

Neat example, but the powershell toolkit allows full access to the SDK so you need not go to so much trouble. :D

Same thing using toolkit: "get-viserver $vcenter; get-vm"

If you want to access the methods and properties on the SDK objects, you can use the get-view cmdlet to do so.

Post a Comment