vCAC: Displaying Available Networks per Location using PowerShell

Sometimes you want to have the user to be able to select a VLAN based on the location he chooses during a machine request. With some little logic you can actually create a DropDown box backed by a ValueExpression that lists all selected networks per location. You only have to enumerate all HostReservations, lookup the location of its related Compute Resource and read each selected network via its link from the reservation to the host:

PS > $m.GetType().FullName;
DynamicOps.ManagementModel.ManagementModelEntities

$al = New-Object System.Collections.ArrayList;
foreach($hr in $m.HostReservations) {
  Write-Host ("Processing HostReservation '{0}' [{1}] ..."
    -f $hr.HostReservationName, $hr.HostReservationID)

  $null = $m.LoadProperty($hr, 'Host');
  $h = $hr.Host;
  $hp = $m.LoadProperty($h, 'HostProperties') |
    Select-Object PropertyName, PropertyValue;
  $Location = ($hp |? PropertyName -eq 'Vrm.DataCenter.Location').PropertyValue.ToUpper();
  Write-Host ("Processing HostReservation '{0}' [{1}]: HostName '{2}' has [{3}] location '{4}'."
    -f $hr.HostReservationName, $hr.HostReservationID, $h.HostName, $h.HostID, $Location);

  $anr = $m.LoadProperty($hr, 'HostNicToReservations');
  foreach($nr in $anr) {
    $ahn = $m.LoadProperty($nr, 'HostNic');
    foreach($hn in $ahn) {
      Write-Host ("{0}: {1}" -f $Location, $hn.HostNicName);
      $HostNicName = $hn.HostNicName;
      $NewEntry = [PSCustomObject] @{Name=$Location; Value=$HostNicName;};
      $fExist = $false;
      foreach($l in $al) {
        if( ($l.Name -eq $NewEntry.Name) -And ($l.Value -eq $NewEntry.Value) ) {
          $fExist = $true;
        } #if
      } # foreach
      if(!$fExist) {
        $null = $al.Add($NewEntry);
      } # if
    } # foreach
  } #
} # foreach
ConvertTo-VcacArrayOfProperties
  -InputObject $al
  -FilterName 'Vrm.DataCenter.Location'
  -FilterValue Name
  -Value Value;

The last call is to the Cmdlet contained in our “biz.dfch.PS.vCAC.Utilities” module to automatically create a valid XML ValueExpression as described in our previous post Creating vCAC ValueExpressions automatically.

A side note regarding the network names: within vCAC the network names appear exactly as they were entered per Compute Resource (vCenter). So when you have more than one Compute Resource tagged with the same location field it might happen that you spelled the networks differently in each vCenter. Then you would have to sanitise the names by lookup them up from a common mapping table or similar.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: