VMware Cloud Community
Balaji3110
Contributor
Contributor

Formatting powerCLI output for NSX Edge interface

Hi All,

I am trying to gather the vnic interface details of all NSX edge, DLR in my environment.

I am using foreach loop twice to gather the information. But , i need to format the output like in CSV as below.

Expected Output:

DLR/ESG  Name               Interface Name      Interface connection status          IP address          Mask

ESG 1                                   vnic2                    Connected                                    10.10.10.1           255.255.255.0

ESG 1                                   vnic3                    Connected                                    10.10.10.2           255.255.255.0                             

=================================================================================================================================

$edgelist = get-nsxedge | select name

Write-Host "NIC Information of each DLR/ESG"

Write-Host "-------------------------------"

foreach ($edge in $edgelist)

{

Write-Host "$edge.name"

$interfacelist = Get-nsxedge -name $edge.name | get-nsxedgeinterface

Write-Host "Interface Count:" $interfacelist.count

    foreach ( $interface in $interfacelist)

    {

        Write-Host "Interface Name :" $interface.name

        Write-Host "Interface Type :" $interface.type

        Write-Host "Interface Connection status :" $interface.isconnected

        $priaddress =  $interface | Get-nsxedgeinterfaceaddress | Select PrimaryAddress

$netmask =  $interface | Get-nsxedgeinterfaceaddress | Select subnetMask

Write-Host "IP Address :"  $priaddress

Write-Host "SubnetMask :"  $netmask

Write-Host "----------------------------------------------------------------------------"

    }

   Write-Host "----------------------------------------------------------------------------"

  }

Reply
0 Kudos
5 Replies
LucD
Leadership
Leadership

Try something like this

Get-nsxedge -PipelineVariable edge |

Get-NsxEdgeInterface -PipelineVariable if |

Get-NsxEdgeInterfaceAddress |

Select @{N='DLR/ESG  Name';E={$edge.Name}},

  @{N='Interface Name';E={$if.Name}},

  @{N='Interface connection status';E={$if.IsConnected}},

  @{N='IP address';E={$_.PrimaryAddress}},

  @{N='Mask';E={$_.SubnetMask}} |

Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Balaji3110
Contributor
Contributor

Thank you , LuCD.

It worked well and i got a CSV output. But the interface name and connection status seems to be empty. can you help please?output.PNG

Reply
0 Kudos
LucD
Leadership
Leadership

Can you try it like this?

Get-nsxedge -PipelineVariable edge |

ForEach-Object -Process {

    Get-NsxEdgeInterfaces -EdgeId $edge.Id |

    Select @{N='DLR/ESG  Name';E={$edge.Name}},

        @{N='Interface Name';E={$_.Name}},

        @{N='Interface connection status';E={$_.Status}},

        @{N='IP address';E={$_.IP}},

        @{N='Mask';E={$_.Prefix}}

} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Balaji3110
Contributor
Contributor

I tried the above one and this time ended up " Get-nsxedgeinterface " asking for input parameter and i passed $edge , which results in invalid value in the output report.

The output of Get-nsxedgeinterface also has several objects so ,it might need to undergo another foreach (this is for each interface under every ESG/DLR)

So i modified the code as below and now the issue is "get-nsxedgeinterfaceaddress" parameter does not accept any parameter input. Any help on this please?

===========================================

Get-nsxedge -PipelineVariable edge |

ForEach-Object -Process {

Get-NsxEdgeInterface -Edge $edge -PipelineVariable interface |

ForEach-Object -Process {

Get-NsxEdgeInterfaceAddress |

  Select @{N='DLR/ESG  Name';E={$edge.Name}},

              @{N='Interface Name';E={$interface.Name}},

              @{N='Interface connection status';E={$interface.isConnected}},

              @{N='IP address';E={$_.IP}},

              @{N='Mask';E={$_.Prefix}}

}

} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture

=====================================================================================

pipeline.PNG

intaddress.PNG

Reply
0 Kudos
LucD
Leadership
Leadership

Sorry, I don't have an NSX set up right now to do some more tests.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos