Complete network-setup on ESX host with vSphere powerCLI


Me and my colleague Stefan Lozancic had the task to configure the networks on our new Vsphere esx hosts. It was 14 hosts and 54 vlans, 1 vmotion and 1 management-network on each host that had to be configured. We used vSphere powerCLI to do it automated. First we populated a csv-file with all the vlans from one of our old hosts.

#Export Vlan from an esx host
Get-VirtualPortGroup * | Where-Object {$_.vmhostid -like "HostSystem-host-1555" -and $ -like "*vlan*"} | select vlanid,name | Export-Csv c:\temp\vlan2.csv -NoTypeInformation -NoClobber

After that we rebuild the vmotion and production network from scratch and populated the production network with the vlans from the csv. Since the hosts where prepared by two different person some hosts had the management network cards in an active/passive mode, so we made a check and changed those to active/active.

#NAME: CreateHostNetwork.ps1
#AUTHOR: Viktor Lindström and Stefan Lozancic
#COMMENTS: A script we used to stage our network
#setup on our Vspehre hosts
#Deletes default vm network.
$portremove = Get-VirtualPortGroup -name "VM Network"
Remove-VirtualPortGroup -VirtualPortGroup $portremove -Confirm:$false

#Put all nics in "management network" in active mode.
$passivenic = Get-NicTeamingPolicy -VirtualSwitch vswitch0 | where standbynic -NotLike $null
Get-NicTeamingPolicy -VirtualSwitch vswitch0 | Set-NicTeamingPolicy -MakeNicActive ($passivenic).StandbyNic

#Create new virtual switch for vmotion network
New-VirtualSwitch -name vSwitch1 -nic vmnic1,vmnic3 

#Create new virtual port group for vmotion
$ip = Read-Host "which IP for vmotion "
New-VMHostNetworkAdapter -PortGroup Vmotion -VirtualSwitch vSwitch1 -IP $ip -SubnetMask -VMotionEnabled:$true

#Create new virtual swtch for production networkn network
New-VirtualSwitch -name vSwitch2 -nic vmnic4,vmnic5 -NumPorts 256

#Create All Vlans for production 
$Import=Import-Csv C:\temp\vlan2.csv
    foreach ($nat in $Import)
    New-VirtualPortGroup -Name $nat.Name -VirtualSwitch vswitch2 -VLanId $nat.VLanId