Adding vMotion VMKernel Interfaces En Masse

Here's a quick one-liner that I've been particularly pleased by.  I put this together when I was reconfiguring several c7000 chassis at a time and wanted to minimize the amount of typing that I was doing per host.  This is a big ugly one liner, but what it does is to create a new VMK interface on the specified Port Group, assign it a valid IP Address and enable vMotion for that interface.  The command performs those tasks on all ESXi hosts in the "unconfigured" folder.

How does it get the valid IP Address?  Well, it takes the first 2 octets of the host's management IP address, uses a customized 3rd octet, then appends the last octet from the host's management IP address.  So, for example, if the host is managed at 192.168.1.101, the script will use 192.168.#.101 for the vMotion interface.

In this example, it uses a port group called "vMotion" on a VDSwitch that ends with Intranet, and has a standard /24 network with 2 in the third octet for vMotion.

Get-folder unconfigured | get-vmhost | % {$_ | new-vmhostnetworkadapter -virtualSwitch (Get-VDSwitch -name "*Intranet") -PortGroup "vMotion" -IP "$($_.networkinfo.virtualnic.ip.split('.')[0]).$($_.networkinfo.virtualnic.ip.split('.')[1]).2.$($_.networkinfo.virtualnic.ip.split('.')[3])" -subnetmask 255.255.255.0 | set-vmhostnetworkadapter -vmotionenabled:$true -confirm:$false}

Since that's so ugly, here it is broken up into several lines to be more human friendly (and, execute more swiftly):
$vdSwitch = get-vdswitch -name "*intranet"
$vMotionPortGroup = "vMotion"

foreach ($thisHost in (Get-folder Unconfigured | get-vmhost){
$manIP = $thisHost.networkfinfo.virtualnic.ip.split('.')
$vMotionIP = "$($manIP[0]).$($manIP[1]).2.$($manIP[3])"
$vMotionNIC = $thisHost | new-vmhostnetworkadapter -virtualSwitch $vdSwitch -portGroup $vMotionPortGroup -IP $vMotionIP -subnetMask 255.255.255.0
$vMotionNIC | set-vmhostnetworkadapter -vmotionenabled:$true -confirm:$false
}

Comments

Popular posts from this blog

Orphaned VMDK Files

Deleting Orphaned (AKA Zombie) VMDK Files

Clone a Standard vSwitch from one ESXi Host to Another