ESXi Host vSwitch Network Connectivity Validation

One of my customers is standing up a lot of new resources.  The Distributed vSwitch makes it really easy to configure the ESXi hosts correctly with all of the necessary VLANs, but how do you know that they're fully plumbed end to end?  The best thing that I can think of is to simply test it; stand up a VM on each Port Group and validate its network connectivity.  Of course, you've only got two or three VLANs to test, right?  Yeah, right.  This customer has a few dozen Port Groups to validate, and since I'm not interested in wearing out my mouse, I decided to script things.

To accomplish this particular task, I actually wrote two scripts.  One of them is designed to run on a test VM and changes its network identity (IP Address and Default Gateway) and runs the ping test, the other runs on an administrator's workstation and changes the Port Group that the test VM is using.  After each script makes its required change, it prompts the administrator to proceed (so that you can make sure that the other script has performed its steps and so that you can do any required troubleshooting).

As input, the scripts simply take a CSV with two columns: PG and IP.  So, you basically make a table with Port Group names in the PG column and then a valid IP address for each network in the IP column.  The Change-PG script reads in that CSV and moves the VM onto the Port Group defined on the first line; then you execute the Change-IP script which reads in that same CSV and sets the VM to use the IP on the first line and then do a ping test.  Once the test has come back successful, you tell the Change-PG script to proceed and then the Change-IP script in order to repeat the test for the next network.  This customer consistently uses .254 as their Gateway addresses, so the script is hard coded to do that.

It's not a perfect solution as it still involves having someone sit there, babysitting the scripts to make sure that they stay synchronized (there's an experimental cmdlet called Set-VMGuestNetworkInterface that can do this through the VMTools, but I didn't want to enter my credentials into a script and so didn't go that route), but it still beats the pants off of manually typing in all of that data and stepping through the wizards that many times.  The scripts are below, with the usual caveats.  They worked fine for me in this particular situation, but there's no guarantee that they'll work for you, etc.

#Run on admin workstation to change VLAN
$testVM = "VM-Name"
$allPGs = import-csv E:\Scripts\port_group_addressses.csv
foreach ($thisPG in $allPGs.PG)
{
   echo "====================="
   echo "Moving VM onto $thisPG"
   echo "====================="
   get-vm $testVM | Get-NetworkAdapter | set-networkadapter -confirm:$false -portgroup $thisPG
   do
   {
      $proceed = read-host "Proceed [n|y]"
   } until ($proceed -like "y*")
}


#Run on test VM to change its IP
#IP Setting technique from http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/28/use-powershell-to-configure-static-ip-and-dns-settings.aspx
$allIPs = import-csv E:\Scripts\port_group_addressses.csv
$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'"
foreach ($thisIP in $allIPs.IP)
{
   $proceed = "n"
   echo "====================="
   echo "Setting IP to $thisIP"
   echo "====================="
   $wmi.EnableStatic($thisIP, "255.255.255.0")
   $wmi.SetGateways("$($thisIP.substring(0,$thisIP.LastIndexOf("."))).254", 1)
   start-sleep 10
   do
   {
      ping 4.2.2.2
      $proceed = read-host "Proceed [n|y]"
   } until ($proceed -like "y*")
}

Comments

Popular posts from this blog

Clone a Standard vSwitch from one ESXi Host to Another

PowerShell Sorting by Multiple Columns

Deleting Orphaned (AKA Zombie) VMDK Files