CSV format

------------------------------------------------------------------------------------------

wKioL1meR8TgXnPgAAAFnft6ytE267.png


Script

------------------------------------------------------------------------------------------

#[cluster,vSwitch,VLANname,VLANid]

# e.g: cluster name,vSwitch0,VM_network,192.168.0.0,11

 

Add-PSSnapin vmWARE.VimAutomation.Core

$vc="vcntername"
connect-viserver $vc


# Set the input file

$InputFile = "C:\PS\Import_PortGroup\All_PortGroup_SIT_20161202.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

  }

}