Validate Distributed Virtual Switch (DVS) settings on all ESXi hosts from vCenter

To validate Distributed Virtual Switch (DVS) settings on all ESXi hosts from vCenter and check for any issues on specific ports, you can use PowerShell and VMware PowerCLI. The script below demonstrates how to achieve this:

# Connect to vCenter Server
Connect-VIServer -Server <vCenter-Server> -User <Username> -Password <Password>

# Get all ESXi hosts managed by vCenter
$esxiHosts = Get-VMHost

# Loop through each ESXi host
foreach ($esxiHost in $esxiHosts) {
    $esxiHostName = $esxiHost.Name
    Write-Host "Validating DVS settings on ESXi host: $esxiHostName"

    # Get the Distributed Virtual Switches on the host
    $dvsList = Get-VDSwitch -VMHost $esxiHostName

    # Loop through each Distributed Virtual Switch
    foreach ($dvs in $dvsList) {
        $dvsName = $dvs.Name
        Write-Host "Checking DVS: $dvsName on ESXi host: $esxiHostName"

        # Get the DVS Ports
        $dvsPorts = Get-VDPort -VDSwitch $dvs

        # Loop through each DVS port
        foreach ($dvsPort in $dvsPorts) {
            # Check for issues on specific ports (e.g., Uplink ports, VM ports, etc.)
            if ($dvsPort.UplinkPortConfig -eq $null -or $dvsPort.VM -eq $null) {
                Write-Host "Issue found on port: $($dvsPort.PortKey) of DVS: $dvsName on ESXi host: $esxiHostName"
            }
        }
    }
}

# Disconnect from vCenter Server
Disconnect-VIServer -Confirm:$false

Replace <vCenter-Server>, <Username>, and <Password> with your vCenter Server details.

Explanation of the script:

  1. The script connects to the vCenter Server using the Connect-VIServer cmdlet.
  2. It retrieves all ESXi hosts managed by vCenter using Get-VMHost.
  3. The script loops through each ESXi host and gets the Distributed Virtual Switches on each host using Get-VDSwitch.
  4. For each Distributed Virtual Switch, the script checks each port (VM port or Uplink port) to identify any issues using the Get-VDPort cmdlet. In this example, we check for issues where either the UplinkPortConfig or VM properties are null, which could indicate misconfigured or missing ports.
  5. If any issues are found on the ports, the script outputs a message with details of the port, DVS, and ESXi host where the issue was detected.

Please note that this script provides a basic example of DVS validation and may need modifications based on your specific environment and the issues you want to check for. Always thoroughly test any script in a non-production environment before using it in a production environment. Additionally, consider customizing the script further based on your specific DVS configuration and requirements.

Leave a comment