Wednesday, October 22, 2014

PowerCLI - Add PortGroups to Standard vSwitch.

If your environment uses Standard vSwitches it is more likely that you often end up adding a portgroup on all the hosts in a cluster. This becomes even more tedious task when you have multiple portgroups to add.

This script helps you to add new portgroups to an existing vSwitch. You can also connect to an ESXi Host directly if you have a standalone host (which is rare in Prod environments though)..

$vcserver = "vCenterFQDN/IP"
$portvc = "443"
$myvSwitch ="Your vSwitch (EX: vSwitch1)"
Connect-VIServer $vcserver -Port $portvc -Credential (Get-Credential)
$hosts = Get-VMHost -Location "Cluster_Name" | Sort Name
foreach($host in $hosts){

# Enable below line if you want to create a new vSwitch named with $myvSwitch and assign NICs 1,3,4,5 to the vSwitch.
#$host | New-VirtualSwitch -Name $myvSwitch -Nic vmnic1, vmnic3, vmnic4, vmnic5

#Add new portgroups to the vSwitch named "$myvSwitch". Each portgroup/VLAN needs one line similar to the below..
$host | Get-VirtualSwitch -Name $myvSwitch | New-VirtualPortGroup "corp 80" -VLanId 80 -Confirm:$false
$host | Get-VirtualSwitch -Name $myvSwitch | New-VirtualPortGroup "corp 90" -VLanId 90 -Confirm:$false
$host | Get-VirtualSwitch -Name $myvSwitch | New-VirtualPortGroup "corp 100" -VLanId 100 -Confirm:$false
Disconnect-VIServer $vcserver -confirm:$false

***PS: Test it before you run in a Prod Cluster. ***

No comments:

Post a Comment