PowerCLI script to add multiple VLANs to multiple ESXi hosts

Here is a quick script to use to add multiple VLANs to multiple ESXi hosts.

You will need to create a csv file called VLANFile.csv with the following columns:

cluster

vSwitch

VLANname

VLANid

# Set the input file

$InputFile = "C:\Temp\VLANFile.csv"


# Read the import file

$MyVLANFile = Import-CSV $InputFile



# Parse the input file and add the virtual port groups accordingly



ForEach ($VLAN in $MyVLANFile) {

  $MyCluster = $VLAN.cluster

  $MyvSwitch = $VLAN.vSwitch

  $MyVLANname = $VLAN.VLANname

  $MyVLANid = $VLAN.VLANid





  # Query the cluster to retrieve the hosts

  $MyVMHosts = Get-Cluster $MyCluster | Get-VMHost | sort Name | % {$_.Name}



  # Loop through the hosts and add the virtual port group to our vswitch based on the input

  ForEach ($VMHost in $MyVMHosts) {

  Get-VirtualSwitch -VMHost $VMHost -Name $MyvSwitch | New-VirtualPortGroup -Name $MyVLANname -VLanId $MyVLANid

  }

} 

Leave a Reply

Your email address will not be published. Required fields are marked *