PowerCLI: Configuring additional portgroups

Share on:

I had to configure additional portgroups based on vlanids on a vSphere cluster. I couldn’t use host profiles to apply the configuration on all hosts.

 1$vCenter = Connect-VIServer -server(Read-Host "Please enter the name of your vCenter Server.")  
 2$Cluster = Get-Cluster -Name (Read-Host "Please enter the name of the cluster whose servers need to be reconfigured.")  
 3$vSwitch = Read-Host "Please enter the name of the vSwitch on which the portgroups need to be created."  
 4$PGfile = import-csv -Path (Read-Host "Please enter the path to the csv file with the new portgroups to be added to the vSwitch")
 5
 6$Cluster | Get-VMhost | ForEach-Object {  
 7    $vSwitch = Get-VirtualSwitch -Name $vSwitch -Vmhost $_  
 8    $PGfile | Foreach-object {  
 9        $vpg = New-VirtualPortGroup -VirtualSwitch $vSwitch -Name
10        $_.PortGroupName  
11        Set-VirtualPortGroup -VirtualPortGroup $vpg -VLanId $_.Vlanid  
12    }  
13}
14
15Disconnect-VIserver -Server $vCenter -Confirm:$False  

The script asks for

  • vCenter IP or FQDN
  • Cluster Name
  • vSwitch name
  • Path to the csv file

The csv file contains the portgroup names and vlanid’s.

1PortGroupName,Vlanid  
2VLAN900,900  
3VLAN901,901  
4VLAN902,902