Using PowerCLI to add VLAN to multiple ESXi hosts

So I got tired of creating new VLANs on multiple ESX-hosts every time there was a need for new network in the datacenter.
And since SSH is disabled, according to VMware best practices, I had to search for a method to add vlan to the vswitch through command line.

PowerCLI

Some research yielded an interesting discovery. A tool called PowerCLI, which is cmdlets for PowerShell.
You need at least PowerShell 3.0 to install PowerCLI v6.0 (Download PowerCLI).

PowerCLI commands

The command of interest in this scenario is New-VirtualPortGroup.
This command creates a new VirtualPortGroup, i.e. a VLAN. The commands can be run directly through PowerCLI without using the AddVlan.ps1-script.

Connect-VIServer vcenter01.localnetwork.local
get-cluster -name datacenter01 | Get-VMHost | Get-VirtualSwitch -name “vSwitch1” | New-VirtualPortGroup -Name “Srv.SecureServers” -VLanId “1337”

powercli
powercli2

A script to create VLANs to all hosts within a cluster

Be warned: You should verify this script works in test environment and fully understand the commands prior to using it in a production environment.

# AddVlan.ps1
#
# A script to automatically add a VLAN to all hosts within a cluster.
# Requires knowledge of vCenter, Cluster-name and what vSwitch needs to be configured.
#
# CHANGELOG
# v1.0 – gos@networkoc.net – Creation of the script
# v1.1 – gos@networkoc.net – Added a section for default values which may be commented out.
#

# Initialize the PowerCLI to grant access to VMware CMDlets
& “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1”

# Disclaimer – “You are on your own”
write-host “IMPORTANT” -ForegroundColor red -BackgroundColor Black
write-host “This script is meant to be used by professional users only.” -ForegroundColor red -BackgroundColor white
write-host “This script does NOT sanitize input – so do not write anything wrong now. ,)” -ForegroundColor red -BackgroundColor white
write-host “USE AT OWN RISK.” -ForegroundColor red -BackgroundColor White

# Get inputs from user
$vCenter = Read-Host -Prompt ‘Input your vCenter server here’
$cluster = Read-Host -Prompt ‘Input your cluster here’
$switch = Read-Host -Prompt ‘Input your vSwitch here (e.g. vSwitch1)’
$vlanname = Read-Host -Prompt ‘Input the new VLAN name here’
$vlanid = Read-Host -Prompt ‘Input the new VLAN ID here’

# Get inputs from user, with default values
#
# Uncomment this section and comment out “get inputs from user” above
# if you would like to use default values
#
# $vCenter = Read-Host -Prompt ‘Input your vCenter server here (Press enter for vcenter01.localnetwork.local)’
# $cluster = Read-Host -Prompt ‘Input your cluster here (Press enter for datacenter01)’
# $switch = Read-Host -Prompt ‘Input your vSwitch here (Press enter for vSwitch1)’
# $vlanname = Read-Host -Prompt ‘Input the new VLAN name here’
# $vlanid = Read-Host -Prompt ‘Input the new VLAN ID here’
#
# Assign default values to the field if no user input was given
# If(-not($vCenter)){$vCenter = “vcenter01.localnetwork.local”}
# If(-not($cluster)){$cluster = “datacenter01”}
# If(-not($switch)){ $switch = “vswitch1”}

# Connect to vCenter
Connect-VIServer $vCenter

# Get the hosts of cluster with the corresponding vSwitch-name and create Virtual Port Groups
get-cluster -name $cluster | Get-VMHost | Get-VirtualSwitch -name $switch | New-VirtualPortGroup -Name $vlanname -VLanId $vlanid

References:
VMware – PowerCLI https://developercenter.vmware.com/tool/vsphere_powercli/6.0
PowerCLI command help – https://www.vmware.com/support/developer/PowerCLI/PowerCLI41/html/New-VirtualPortGroup.html

Hopefully this will make someones life a bit easier. =)

5.00 avg. rating (99% score) - 1 vote

One Response to Using PowerCLI to add VLAN to multiple ESXi hosts

Leave a Reply

Your email address will not be published. Required fields are marked *