VMM Script Series, part 2 – Get-NetworkMap

I’m publishing some of the scripts I have written for VMM.  These are each based on specific customer asks or conversations that lead me to go sleepless in trying to solve the problems myself.  I am very interested in your feedback, especially if you would like to offer a change to improve efficiency or effectiveness.

Get-NetworkMap

This script is designed to gather data on how each VM is physically connected and present it in such a way that an admin could quickly track down the network path for troubleshooting purposes.  For each VM, the script provides which host it is on, the virtual network name, vlan id, adapter type, the name of the physical device where the virtual network is assigned, and the physical network connection name for that device.

If the VM has more than one network adapter, each VM is displayed within the properties of that VM in the output.  Commas separate the individual values so if you have several adapters you may need to consider modifying the script to output in a more friendly way.  With 2-3 adapters, tracing the data across fields and keeping it straight is trivial.

I tried a lot of approaches at how this data should be presented and how the script should run.  I settled on treating the script like a commandlet and passing command line arguments.  So all you need to do is open PS somewhere that you have the console installed and run the script by passing it your VMM server name.  The nice thing about this is the output can be stored to a new array to be worked in to other solutions.

example, “$netinfo = get-networkmap.ps1 v-vmm-01”

or dig a lot deeper, such as reporting the physical MAC address where the network for each VM is bound.

foreach ($record in $netinfo){write-host ""$record.name" - "$(foreach ($nic in $record.device){$nic.macaddress})""}

Notes

In each of the scripts for this series, I have added a line to load the VMM snap-in.  This way it can be run from any machine, including a workstation, where the VMM console is installed by right clicking on the file and choosing “run”.  If you run the script from within VMM you will get a few lines of error text that the snap-in is already loaded.  You can either ignore this, or comment out the line if it bothers you.

Screen Shots

image

That’s all there is to it!  See the very first listing, multiple values for each field because more than 1 adapter is assigned.

Script

disclaimer: this script is not supported in any way. I have posted the code rather than the .ps1 file so that you can review it, modify it to make it your own, and test it before trusting it in a production environment. now this is your code, I am not responsible for its use.

 

    1: # ------------------------------------------------------------------------------
    2: # Network Map
    3: # ------------------------------------------------------------------------------
    4: # blogs.technet.com/offcampus
    5: # version 1.0
    6: #
    7: # Description
    8: #  Useful for displaying the physical network adapter information for each VM
    9: #
   10: # Command Arguements
   11: #  The script expects the VMM server name to be provided as an arguement
   12: #
   13: # ------------------------------------------------------------------------------
   14:  
   15: # All variables in the script with customizable values are mapped here for convenience
   16: $VMMServerName = $args[0]
   17:  
   18: # Load Snap-Ins
   19: Add-PSSnapin Microsoft.SystemCenter.VirtualMachineManager 
   20:  
   21: # Begin Script
   22: Get-VMMServer $VMMServerName | out-null
   23: $VM = Get-VM
   24:  
   25: $NetworkMap = @()
   26: foreach ($entry in $VM)
   27: {
   28:       $nic = (Get-VirtualNetworkAdapter -vm $entry.Name)
   29:       $hostadapters = foreach ($adapter in $nic) {get-virtualnetwork -vmhost $entry.VMHost -name $adapter.VirtualNetwork}
   30:       $connection = foreach ($network in $hostadapters) {$network.VMHostNetworkAdapters}
   31:       $hostnetworkadapters = $hostadapters.VMHostNetworkAdapters
   32:       
   33:       $netinfo = "" | Select-Object Name, HostName, VirtualNetwork, Type, VLAN, Device, Connection
   34:       $netinfo.Name=$entry.Name
   35:       $netinfo.HostName=$entry.VMHost
   36:       $netinfo.VirtualNetwork=foreach($virtualnetwork in $hostadapters){$virtualnetwork.Name}
   37:       $netinfo.Type=foreach($type in $nic){$type.VirtualNetworkAdapterType}
   38:       $netinfo.VLAN=foreach($vlan in $nic){$vlan.VLanId}
   39:       $netinfo.Device=foreach($device in $hostadapters){$device.VMHostNetworkAdapters}
   40:       $netinfo.Connection=foreach($nc in $connection){$nc.connectionname}
   41:       $NetworkMap += $netinfo
   42:       }
   43:  
   44: $NetworkMap