<# Required parameter: -SwitchName Name of the existing standard vSwitch where the port groups will be created. Optional parameter: -BaseVlanId Starting VLAN ID. Default is 100. The script creates: WS2024-LX-WAN-Linux = BaseVlanId WS2024-LX-INT-Linux = BaseVlanId + 1 WS2024-LX-DMZ-Linux = BaseVlanId + 2 Example: .\Create-PortGroups.ps1 -SwitchName vSwitch0 #> [CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$SwitchName, [ValidateRange(1, 4092)] [int]$BaseVlanId = 100 ) $ErrorActionPreference = 'Stop' $switch = Get-VirtualSwitch -Name $SwitchName -ErrorAction Stop $portGroups = @( @{ Name = 'WS2024-LX-WAN-Linux'; VlanId = $BaseVlanId }, @{ Name = 'WS2024-LX-INT-Linux'; VlanId = $BaseVlanId + 1 }, @{ Name = 'WS2024-LX-DMZ-Linux'; VlanId = $BaseVlanId + 2 } ) foreach ($portGroup in $portGroups) { $existing = Get-VirtualPortGroup -VirtualSwitch $switch -Name $portGroup.Name -ErrorAction SilentlyContinue if ($existing) { if ($existing.VLanId -ne $portGroup.VlanId) { Set-VirtualPortGroup -VirtualPortGroup $existing -VLanId $portGroup.VlanId | Out-Null Write-Host "Updated $($portGroup.Name) VLAN to $($portGroup.VlanId)" } else { Write-Host "$($portGroup.Name) already exists" } continue } New-VirtualPortGroup -VirtualSwitch $switch -Name $portGroup.Name -VLanId $portGroup.VlanId | Out-Null Write-Host "Created $($portGroup.Name) with VLAN $($portGroup.VlanId)" }