Automated Creation of VMware PortGroups

I was working on creating a new lab environment for Exchange 2013 and Lync 2013. I decide to create a couple of new portgroups with VLANs on all the ESXi servers. the task was going to be repeated 30 times so I decided to spend a little time creating a script to automate the process of creating all the port groups and the vlans.

[sourcecode]

############################################################
# ScriptName: Configure_PortGroups.ps1 #
# Function : Automate vSwitch0 PortGroup Creation #
# Author : Carlos Vargas #
############################################################

# Check is VMware Snapin is loaded
if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )
{
Write-Host "Adding VMware PowerCLI SnapIn"
Add-PsSnapin VMware.VimAutomation.Core
}

# Define the Credentials
# Define the Username
$user = Read-host -Prompt "Type your username. Ex user1 or Domainuser1 or user@domain.com"
# Define the secure password
$pass = Read-host -Prompt "Type your password" -AsSecureString
# Convert Credentials to secure string
$mycreds = New-Object System.Management.Automation.PSCredential ($user,$pass)
# Pass $mycreds variable to any PowerShell Commandlet that support PScredentials value.

# Ask for the Host name or IP
$ESXHOST = Read-Host "Enter ESX Host Name or vCenter Server"
Connect-VIServer $ESXHOST -Credential $mycreds

# Get Hosts
$vmhosts = Get-Datacenter | Get-VMhost | Select-Object Name

# loop for the names
foreach ($i in $vmhosts)
{
$vs = Get-VirtualSwitch -VMhost $i.Name -Name vSwitch0
$portgroups = Import-CSV .portgroups.csv
Foreach ($portgroup in $portgroups){
#Write-Host "Portgroup Name: " $portgroup.name " vlanid : " $portgroup.vlanid
$vs | New-VirtualPortGroup -Name $portgroup.name -VLanID $portgroup.vlanid
}
}

[/sourcecode]

Hope this helps you. Drop me a comment and let me know what you think

Subscribe For Latest Updates

Sign up for best of digital marketing, livestream and technology opinions on what matters to you.

Invalid email address
We promise not to spam you. You can unsubscribe at any time.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: