vmware-archive / powernsx

PowerShell module that abstracts the VMware NSX-v API to a set of easily used PowerShell functions
173 stars 90 forks source link

Get Related Objects in Logical Switch? #603

Closed haripetrov closed 4 years ago

haripetrov commented 4 years ago

Is there a way to get the related objects (e.g. Edges, VMs) from a logical switch in NSX?

alagoutte commented 4 years ago

Hi @haripetrov

For me, there is no API call for this

haripetrov commented 4 years ago

Now we are writing PowerShell script which will extract this information using the current NSX cmdlets. Soon should be ready and once we have information I will share it here.

haripetrov commented 4 years ago

This is the PowerShell script that we managed to compile:

[CmdletBinding(PositionalBinding=$false)]

Param (
[parameter(Position= 0, Mandatory = $false)]
[string]$VIServer = "vCenter",
[string]$Path     = "C:\NSX\NSXReport.csv",
[string]$PathExportNsxLogicalRouter     = "C:\NSX\NSXLogicalRouter.csv"
)

begin
{

    If ( ! (Get-module PowerNSX )) {
    Import-Module PowerNSX
    }

# connecting to the NSX server
$connection = Connect-NSXServer -vCenterServer $VIServer
$defaultNsxConnection = $connection
$defaultViServer = $connection.viConnection
}

process 
{
# Getting NSX Edge information
$getEdge = get-nsxedge | Get-NsxEdgeInterface 

$edge = $getEdge | select name,edgeId,portgroupName
$edgeEdgeSub = $getEdge | Get-NsxEdgeSubInterface 

 # Getting NSX Logical Router information
 $getNsxLogicalRouter = Get-NsxLogicalRouter | Get-NsxLogicalRouterInterface | select connectedToId,logicalRouterId,connectedToName,type

 $output =    foreach ( $ls in Get-NsxLogicalSwitch ) {
        $pg = $ls | Get-NsxBackingPortGroup
        foreach ( $portgroup in $pg) { 
            $vm = $portgroup| Get-VM
            foreach ( $virtualmachine in $vm) { 
                    $vlookup = $edge | where {$_.portgroupName -like $ls.name}
                    $vlookupEdgeSub = $edgeEdgeSub | where {$_.logicalSwitchName -like $ls.name}

                 [pscustomobject]@{
                    "vCenter" = $defaultViServer.name
                    "NSX" = $defaultNsxConnection.server
                    "LS_ObjectID" = $ls.objectId
                    "LS_Name" = $ls.name
                    "LS_vdnId" = $ls.vdnId
                    "EdgeID" = $vlookup.edgeId
                    "EdgeVNIC" = $vlookup.name
                    "EdgeTrunk_LS_ID" = $vlookupEdgeSub.logicalSwitchId
                    "EdgeTrunk_LS_Name" = $vlookupEdgeSub.logicalSwitchName
                    "EdgeTrunk_LS_isConnected" = $vlookupEdgeSub.isConnected
                    "LS_tenantId" = $ls.tenantId
                    "BackingPortGroup" = $portgroup.name
                    "VirtualMachine" = $virtualmachine.name
                 } # END pscustomobject
            }
        }
    }

$getNsxLogicalRouter | export-csv $PathExportNsxLogicalRouter -NoTypeInformation
$output | export-csv $Path -NoTypeInformation
}

end
{
Disconnect-NsxServer
}