PowerCLI: Configuring additional portgroups

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.

$vCenter = Connect-VIServer -server(Read-Host "Please enter the name of your vCenter Server.")  
$Cluster = Get-Cluster -Name (Read-Host "Please enter the name of the cluster whose servers need to be reconfigured.")  
$vSwitch = Read-Host "Please enter the name of the vSwitch on which the portgroups need to be created."  
$PGfile = import-csv -Path (Read-Host "Please enter the path to the csv file with the new portgroups to be added to the vSwitch")

$Cluster | Get-VMhost | ForEach-Object {  
    $vSwitch = Get-VirtualSwitch -Name $vSwitch -Vmhost $_  
    $PGfile | Foreach-object {  
        $vpg = New-VirtualPortGroup -VirtualSwitch $vSwitch -Name
        Set-VirtualPortGroup -VirtualPortGroup $vpg -VLanId $_.Vlanid  

Disconnect-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.


Share this post: