samedi 11 juin 2011

POWERCLI: move ESX/ESXi NICs to a physical switch using its name

· 0 commentaires

Hi all,
I decided to publish on this blog some of my powercli scripts. Google and a lot of blogs already saved me using powercli, so let me share also my experience.

The code of my scripts is not perfect but it is working.


So let's start with a recent issue we had in our environment.
All our ESX/ESXi servers are blades. Each of them has 4 NICs.
2 NICs for the VM and 2 NICs for the console. Of course each NIC used for one function is connected to a different Cisco Switch Stack. That allows us to have network redundancy.

Recently, we had an issue with one of these network stack, so we wanted to move all the active NICs to the second stack. Unfortunately, all our blades are not the same model, and thus the VMNIC teamings are different. So the problem was more complex than just moving all the vmnic0 and vmnic2 as active, and the vmnic1 and vmnic3 as standby.
Hopefully for us, ESX/ESXi provide information on which network switch are connected your NICs, usind CDP (Cisco Discovery Protocol).

So I wrote this little script. The idea is to give as input part of the name of the network switch you want to keep active. This part of the name must be unique of course. The script will then list all the NICs not connected to this switch and move it as standby.

So here is the code:

# $hostlist must contain the list of the hosts on which you want to execute the code
# use any get-vmhost command to fill in this list

# $objSwitchName must contain a part of the name of the network switch you want to failover the NICs
# this part of the name must be unique for each switch

$objSwitchName="SwitchName"

foreach ($objhost in $hostlist) {

# Getting the vSwitches of the host (only 2 in all our servers)
$objVMVswitch = Get-VirtualSwitch -VMhost $objhost

# Put both NICs back active for VM vSwitch
$objVMteaming = Get-NicTeamingPolicy $objVMVswitch[1]
$objNic = $objVMteaming.StandbyNic
Set-NicTeamingPolicy $objVMteaming -MakeNicActive $objNic

# Put both NICs back active for ESX console vSwitch
$objVMteaming = Get-NicTeamingPolicy $objVMVswitch[0]
$objNic = $objVMteaming.StandbyNic
Set-NicTeamingPolicy $objVMteaming -MakeNicActive $objNic

# Generate an array that contains the list of NICs not connected to $objSwitchName
# This list is generated using CDP information of the ESX
$objNicInSwitch = New-Object System.Collections.ArrayList
$objNicInSwitch.Clear()
Get-VMHost $objHost |
%{Get-View $_.ID} |
%{$esxname = $_.Name; Get-View $_.ConfigManager.NetworkSystem} |
%{ foreach($physnic in $_.NetworkInfo.Pnic){
$pnicInfo = $_.QueryNetworkHint($physnic.Device)
foreach($hint in $pnicInfo){
if( $hint.ConnectedSwitchPort.DevId -notlike "*$objSwitchName*" ) {
Write-Host $esxname $physnic.Device
$hint.ConnectedSwitchPort.DevId
$objNicInSwitch.add($physnic.Device)
}
}
}
}

# Now we will move to standby the NICs listed in previous step
# This is a loop that collect the NICs in each vSwitch
# It checks if the NIC is listed in the array $objNicInSwitch
# If yes, then it move the NIC to standby
foreach($phynic in $objNicInSwitch)
{
$objVMVswitch = Get-VirtualSwitch -VMhost $objhost
$objVMteaming = Get-NicTeamingPolicy $objVMVswitch[1]
$objNic = $objVMteaming.ActiveNic
foreach ($teamnic in $objNic)
{
write-host $phynic
write-host $teamnic
if ($phynic -eq $teamnic)
{
Set-NicTeamingPolicy $objVMteaming -MakeNicStandBy $phynic
}
}
$objVMteaming = Get-NicTeamingPolicy $objVMVswitch[0]
$objNic = $objVMteaming.ActiveNic
foreach ($teamnic in $objNic)
{
write-host $phynic
write-host $teamnic
if ($phynic -eq $teamnic)
{
Set-NicTeamingPolicy $objVMteaming -MakeNicStandBy $phynic
}
}
}
}


If you have any questions about this code, don't hesitate to comment the article.

Read More......

A propos de moi

Je suis David Vekemans. Ingénieur en informatique de 32 ans, je vais à travers ce blog essayer de vous faire partager mon expérience en résolution de problèmes. Cela va couvrir mes différents centres d'intérêts : l'informatique et Windows, la photographie numérique, la réception satellite.

Visites