Export Networks to CSV

I am working on a project that requires gathering configurations. This script will log into the vCenter and export the Network configuration to a CSV.

# Import VMware PowerCLI module
Import-Module VMware.PowerCLI

# Suppress invalid certificate warnings
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

# Connect to vCenter Server
$vCenterServer = "your-vcenter-server"
$username = "your-username"
$password = "your-password"
Connect-VIServer -Server $vCenterServer -User $username -Password $password

# Retrieve Standard Portgroups
Write-Host "Fetching Standard Portgroup details..."
$standardPortGroups = Get-VirtualPortGroup -Standard | Select-Object Name, @{Name="VLAN"; Expression={
    if ($_.VlanId -eq 0) { "VLAN Trunking or Untagged" } else { $_.VlanId }
}}, @{Name="Description"; Expression={$_.Notes}}

# Retrieve Distributed Virtual Portgroups
Write-Host "Fetching Distributed Virtual Portgroup details..."
$distributedPortGroups = Get-VDPortgroup | Select-Object Name, @{Name="VLAN"; Expression={
    if ($_.VlanId -eq 0) { "VLAN Trunking or Untagged" } else { $_.VlanId }
}}, @{Name="Description"; Expression={$_.Description}}

# Combine both results into a single list
$allPortGroups = $standardPortGroups + $distributedPortGroups

# Display the network information
Write-Host "Network Details:"
$allPortGroups | ForEach-Object {
    Write-Host "Name: $($_.Name)"
    Write-Host "VLAN: $($_.VLAN)"
    Write-Host "Description: $($_.Description)"
    Write-Host "------------------------------"
}

# Export the network details to a CSV file
$outputFile = "NetworksDetails.csv"
$allPortGroups | Export-Csv -Path $outputFile -NoTypeInformation -Encoding UTF8
Write-Host "Network details have been exported to $outputFile"

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

Leave a Reply

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

Share on Social Media