PowerCLI Script to assign IP on each host in ESX Cluster

Recently i came across a situation where i had to create and assign IP to vmkernel portgroup (for vmotion) for 3300 ESX host. So i wrote one basic powercli script to assign IP on each host in ESX Cluster.


PowerCLI Script to create VMKernel portgroup and assign IP on each host in cluster 

#  Note – Replace respective name wherever applicable.  Below sample is for one cluster, you can customize for all cluster in vcenter.

$vmhost = get-cluster 0000 | get-vmhost | sort Name

$pg = $vmhost[0] | get-virtualportgroup | where {$_.name -like ‘vSphere*’}

$subnetprefic=’xxxx’     # e.g. ‘192.168.10’
$ipperserver = 1
$startip = 4

foreach ($esx in $vmhost){

$ipaddress = “$subnetprefix.$($startip + $_)”
$startip += $ipperserver

new-vmhostnetworkadapter -vmhost $esx -virtualswitch $vdswitch -ip $ipaddress -subnet $subnet -portgroup $pg

Hope this short script helps.. Happy reading!!