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.
For each NIC it creates a temporary switch, portgroup, and VMkernel interface:
$Switch1=New-VirtualSwitch -Name "sw_Connectivity_Test" -Nic $Nic
$Portgroup1=New-VirtualPortGroup -Name "pg_Connectivity_Test" -VirtualSwitch $Switch1
#Create VMK Adapter
$vmk1=New-VMHostNetworkAdapter -PortGroup $Portgroup1 -VirtualSwitch $Switch1 -IP $HostIP -SubnetMask $SubnetMask
Then the esxcli functionality is used to ping a given target address:
$esxcli= get-esxcli -V2 #Use the ESXCLI to run the ping from the host
$arguments = $esxcli.network.diag.ping.CreateArgs()
$arguments.host=$TargetIP #Set IP Address to Ping
$arguments.count="2" #How Many Times to Ping
$arguments.interface=$vmk1 #Use the configured VMKernel Interface
Once the test is complete, the temporary virtual network components are removed.
Remove-VMHostNetworkAdapter $vmk1 -Confirm:$false
Remove-VirtualPortGroup $PortGroup1 -Confirm:$false
Remove-VirtualSwitch $Switch1 -Confirm:$false
The full code is available for download (and potential improvement) on GitHub.