PowerCLI To Add Port Groups To Standard Virtual Switches

In this case, I wanted to share a script I use quite frequently. My client base uses the standard virtual switch, so when a new port group needs to be added or when a new host is added it can be a lot of work to get the port groups added. I scripted this process to be efficient in the process. This comes in handy to knock out this type of task quickly. See below for the script. Keep in mind that you will have to edit the port group names and vlan ids. Also be sure that the vswitch is correct. With a little work this data could be imported from a spreadsheet.

 https://github.com/shanermoore/scripts/blob/master/add-stdportgroup.ps1

$VC = Read-Host " Enter vCenter name:"

 

 

Connect-VIServer $VC

 

 

$VMHosts = Get-cluster "Production" | Get-VMHost

foreach ($VMHost in $VMHosts) {

     

      Get-VMHost -name $VMhost | Get-VirtualSwitch -name vswitch2 | New-VirtualPortGroup -name Name-219 -VLanId 219

      Get-VMHost -name $VMhost | Get-VirtualSwitch -name vswitch2 | New-VirtualPortGroup -name Name-1201 -VLanId 1201

      Get-VMHost -name $VMhost | Get-VirtualSwitch -name vswitch2 | New-VirtualPortGroup -name Name-1300 -VLanId 1300

      Get-VMHost -name $VMhost | Get-VirtualSwitch -name vswitch2 | New-VirtualPortGroup -name Name-217 -VLanId 217

      Get-VMHost -name $VMhost | Get-VirtualSwitch -name vswitch2 | New-VirtualPortGroup -name Name-103 -VLanId 103

      Get-VMHost -name $VMhost | Get-VirtualSwitch -name vswitch2 | New-VirtualPortGroup -name Name-104 -VLanId 104

      Get-VMHost -name $VMhost | Get-VirtualSwitch -name vswitch2 | New-VirtualPortGroup -name Name-105 -VLanId 105

      Get-VMHost -name $VMhost | Get-VirtualSwitch -name vswitch2 | New-VirtualPortGroup -name Name-106 -VLanId 106

      Get-VMHost -name $VMhost | Get-VirtualSwitch -name vswitch2 | New-VirtualPortGroup -name Name-107 -VLanId 107

      Get-VMHost -name $VMhost | Get-VirtualSwitch -name vswitch2 | New-VirtualPortGroup -name Name-112 -VLanId 112

      Get-VMHost -name $VMhost | Get-VirtualSwitch -name vswitch2 | New-VirtualPortGroup -name Name-118 -VLanId 118

      Get-VMHost -name $VMhost | Get-VirtualSwitch -name vswitch2 | New-VirtualPortGroup -name Name-2000 -VLanId 2000

      Get-VMHost -name $VMhost | Get-VirtualSwitch -name vswitch2 | New-VirtualPortGroup -name Name-2001 -VLanId 201

      Get-VMHost -name $VMhost | Get-VirtualSwitch -name vswitch2 | New-VirtualPortGroup -name Name-1207 -VLanId 1207

     

   

    }

 

Disconnect-viserver -Server * -Confirm:$false