PowerCLI script to ...
 
Notifications
Clear all

PowerCLI script to add port groups to VSS virtual switch

1 Posts
1 Users
0 Reactions
289 Views
Brandon Lee
Posts: 395
Admin
Topic starter
(@brandon-lee)
Member
Joined: 14 years ago

If you are not running vSphere Distributed Switches, network configuration for port groups needs to be done at the virtual switch level on each VMware ESXi host. You can do this by hand but it is tedious. For consistency and uniformity, it is best done with automation to do it at scale across multiple hosts, especially in a cluster.

Below is a PowerCLI script that will add port groups specified to vSwitch0. Just define your list of VLANs by edited the script under the #Define the list of port groups with their VLAN IDs section:

# Function to check if connected to any vCenter Server
function Test-VCConnection {
    try {
        # Try to disconnect all servers silently to check if any are connected
        Disconnect-VIServer -Server * -Force -Confirm:$false
        return $false
    } catch {
        # If an error occurs, it means no servers are connected
        return $true
    }
}

# Check if already connected to any vCenter Server
$connected = Test-VCConnection

if (-not $connected) {
    Write-Host "Not connected to any vCenter Server. Please connect to a vCenter Server."
    $vCenterServer = Read-Host "Enter the vCenter Server name or IP"
    $vCenterUser = Read-Host "Enter your vCenter username"
    $vCenterPassword = Read-Host "Enter your vCenter password" -AsSecureString

    # Attempt to connect to the vCenter server
    Connect-VIServer -Server $vCenterServer -User $vCenterUser -Password $vCenterPassword

    # Check if the connection was successful
    $connectedServer = Get-VIServer | Where-Object { $_.IsConnected }
    if (-not $connectedServer) {
        Write-Host "Failed to connect to the vCenter Server. Please try again."
        exit
    } else {
        Write-Host "Successfully connected to vCenter Server: $($connectedServer.Name)"
    }
} else {
    Write-Host "Already connected to vCenter Server(s):"
    Get-VIServer | ForEach-Object { Write-Host " - $($_.Name)" }
}

# Prompt for ESXi host or cluster
$targetType = Read-Host "Do you want to add port groups to a single host or a cluster? (Enter 'host' or 'cluster')"

if ($targetType -eq 'host') {
    $esxiHostName = Read-Host "Please enter the name or IP of the ESXi host"
    $target = Get-VMHost -Name $esxiHostName -ErrorAction Stop
} elseif ($targetType -eq 'cluster') {
    $clusterName = Read-Host "Please enter the name of the cluster"
    $target = Get-Cluster -Name $clusterName -ErrorAction Stop
} else {
    Write-Host "Invalid input. Please enter either 'host' or 'cluster'."
    exit
}

# Define the list of port groups with their VLAN IDs
$portGroups = @(
    @{Name = 'Management'; VLANId = 100},
    @{Name = 'Production'; VLANId = 200},
    @{Name = 'Development'; VLANId = 300},
    @{Name = 'Testing'; VLANId = 400},
    @{Name = 'Backup'; VLANId = 500},
    @{Name = 'VM Network'; VLANId = 0}
)

# Loop through each port group and add to the ESXi host's or cluster's vSwitch0
foreach ($pg in $portGroups) {
    if ($target -is [VMware.VimAutomation.ViCore.Impl.V1.Inventory.ClusterImpl]) {
        $esxiHosts = Get-VMHost -Location $target
    } else {
        $esxiHosts = @($target)
    }

    foreach ($esxiHost in $esxiHosts) {
        # Check if the port group already exists
        $existingPG = Get-VirtualPortGroup -VMHost $esxiHost -Name $pg.Name -ErrorAction SilentlyContinue

        if (-not $existingPG) {
            # Add the new port group with the specified VLAN ID
            New-VirtualPortGroup -Name $pg.Name -VirtualSwitch (Get-VirtualSwitch -VMHost $esxiHost -Name 'vSwitch0') -VLanId $pg.VLANId
            Write-Host "Added port group '$($pg.Name)' with VLAN ID '$($pg.VLANId)' to host '$($esxiHost.Name)'."
        } else {
            Write-Host "Port group '$($pg.Name)' already exists on host '$($esxiHost.Name)'. Skipping."
        }
    }
}