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 | `
$vSwitch = Get-VirtualSwitch -Name $vSwitch -Vmhost $_
$PGfile | `
Foreach {
$vpg = New-VirtualPortGroup -VirtualSwitch $vSwitch -Name $_.PortGroupName
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.

Tagged with: , ,
Posted in Uncategorized
IT Pro Summer GP
IT Pro Summer Grand Prix
Microsoft Virtual Academy
Pro-Exchange / Pro-Lync / Pro-Office365

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 158 other followers

%d bloggers like this: