Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Creates PortGroups on Standard vSwitches on all Hosts in a vSphere Cluster
# Load VMware Snapin (if not already loaded)
if (!(Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {
if (!(Add-PSSnapin -PassThru VMware.VimAutomation.Core)) {
# Error out if loading fails
Write-Error "ERROR: Cannot load the VMware Snapin. Is the PowerCLI installed?"
Exit
}
}
# Start Global Definitions
$vSwitches = New-Object System.Collections.ArrayList
$VIServer = "vCenter.test.lan"
$VIClusterName = "myCluster"
$vSwitchName = "vSwitch0"
$PortGroups = @( ("Vlan123", "123"),
("Vlan456", "456"),
("Vlan789", "789"))
# End Global Definitions
# Start vCenter Connection
Write-Host "Starting to Process vCenter Connection to " $VIServer " ..."-ForegroundColor Magenta
$OpenConnection = $global:DefaultVIServers | where { $_.Name -eq $VIServer }
if($OpenConnection.IsConnected) {
Write-Host "vCenter is Already Connected..." -ForegroundColor Yellow
$VIConnection = $OpenConnection
} else {
Write-Host "Connecting vCenter..."
$VIConnection = Connect-VIServer -Server $VIServer
}
if (-not $VIConnection.IsConnected) {
Write-Error "Error: vCenter Connection Failed"
Exit
}
# End vCenter Connection
# Start VI Object Validation
Write-Host "Starting to Process VI Objects in Cluster " $VIClusterName " ..."-ForegroundColor Magenta
If (!(Get-Cluster -Name $VIClusterName -ErrorAction SilentlyContinue)) {
Write-Error "Error: Cluster Not Found"
Exit
}
$VICluster = Get-Cluster -Name $VIClusterName
$ESXiHosts = $VICluster | Get-VMHost | Where-Object { ($_.ConnectionState -eq 'Connected')}
If (($ESXiHosts).Count -lt 1) {
Write-Error "Error: No Connected Hosts in Cluster found"
Exit
}
Else {
Write-Host ($ESXiHosts).Count " Hosts to Process in Cluster " ($VICluster).Name -ForegroundColor Green
ForEach ($ESXiHost in $ESXiHosts){
$vSwitches += $ESXiHost | Get-VirtualSwitch -Name $vSwitchName
}
If (($vSwitches).Count -lt 1) {
Write-Error "Error: No Connected vSwitches on Hosts found"
Exit
}
Else {
Write-Host ($vSwitches).Count " vSwitches to Process on " ($ESXiHosts).Count " Hosts" -ForegroundColor Green
}
}
# End VI Object Validation
# Start Portgroup creation
Write-Host "Starting to Process "($PortGroups).Count " Portgroups on " ($ESXiHosts).Count " Hosts with " ($vSwitches).Count " vSwitches..." -ForegroundColor Magenta
foreach($PortGroup in $PortGroups){
write-host "PortGroup to Process - Name =" $PortGroup[0] " VLAN =" $PortGroup[1]
ForEach ($vSwitch in $vSwitches){
write-host "vSwitch to Process - Name =" $vSwitch.Name " Host =" $vSwitch.VMHost
If (!($vSwitch | Get-VirtualPortGroup -Name $PortGroup[0] -ErrorAction SilentlyContinue)){
Write-Host "PortGroup Will be created..." -ForegroundColor Green
$vSwitch | New-VirtualPortGroup -Name $PortGroup[0] -VLanId $PortGroup[1] | Out-Null
}
Else {
Write-Host "PortGroup Already exists..." -ForegroundColor Yellow
}
}
}
# End Portgroup creation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.