test-ESXi-Network

I was lucky enough to take delivery of some new ESXi hosts recently. After installing them in the datacentre, I wanted to test that the network had been patched correctly. This environment is going to have Distributed vSwitches configured, but I wanted to test the physical connectivity before joining them to vCenter- have the physical NICs been patched to the correct networks?

PowerCLI to the rescue! I put together some code which automates this process. Provided with a hostname and a list of NICs and targets which should respond, the code fires off a ping for each interface in turn and reports back with success/fail messages.

Code

For each NIC it creates a temporary switch, portgroup, and VMkernel interface:

1#Create Virtual Switches
2$Switch1=New-VirtualSwitch -Name "sw_Connectivity_Test" -Nic $Nic
3#Create PortGroups
4$Portgroup1=New-VirtualPortGroup -Name "pg_Connectivity_Test" -VirtualSwitch $Switch1
5#Create VMK Adapter
6$vmk1=New-VMHostNetworkAdapter -PortGroup $Portgroup1 -VirtualSwitch $Switch1 -IP $HostIP -SubnetMask $SubnetMask

Then the esxcli functionality is used to ping a given target address:

1#Test the connection
2$esxcli= get-esxcli -V2 #Use the ESXCLI to run the ping from the host
3$arguments = $esxcli.network.diag.ping.CreateArgs()
4$arguments.host=$TargetIP #Set IP Address to Ping
5$arguments.count="2" #How Many Times to Ping
6$arguments.interface=$vmk1 #Use the configured VMKernel Interface
7$Result=($esxcli.network.diag.ping.Invoke($arguments)).Summary.Recieved

Once the test is complete, the temporary virtual network components are removed.

1#Tidy up- delete all the Networking Components created
2Remove-VMHostNetworkAdapter $vmk1 -Confirm:$false
3Remove-VirtualPortGroup $PortGroup1 -Confirm:$false
4Remove-VirtualSwitch $Switch1 -Confirm:$false

The full code is available for download (and potential improvement) on GitHub.