Testing Network Failover with PowerCLI

Two weeks ago I wrote an article about How To Bring Down A Single NIC In ESX?. In that post you could see that in order to test this you had to go into the console of the ESX and run the commands on the console.

Already then I was thinking, why not do this from PowerCLI, without having to log into each host.

So here we go..

For the examples sake:

VI Server: vcenter.maishsk.local

ESX Host for Testing: esx1.maishsk.local

vmnic for Redundancy test: vmnic2

function Test-NetworkFO ($vmhost, $vmnic, $switch) {
    If ($switch){
    ##Define which NIC should be tested 
    $mynic = Get-VMHostNetworkAdapter -VMHost (get-vmhost $vmhost) | `
    Where-Object { $_.DeviceName -eq $vmnic }

    ##Bring the NIC down 
    Set-VMHostNetworkAdapter -PhysicalNic $mynic -Duplex Half -BitRatePerSecMb 10  
    }

    If (-not $switch){
    ##Define which NIC should be tested 
    $mynic = Get-VMHostNetworkAdapter -VMHost (get-vmhost $vmhost) | `
    Where-Object { $_.DeviceName -eq $vmnic }

    ##Bring the NIC back up 
    Set-VMHostNetworkAdapter -PhysicalNic $mynic -AutoNegotiate
    }
}

# Turn it on
Test-NetworkF0 "vcenter.maishsk.local" "vmnic2" $true

# Turn it off
Test-NetworkF0 "vcenter.maishsk.local" "vmnic2" $false
 

This is a way to test your Network Redundancy for your ESX hosts.

I would like to thank Alan Renouf - who helped me clean up the code and put it into a function.

Have a happy Thanksgiving and a good weekend!