vmware-archive / powernsx

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

Get-NSXBackingPortgroup has no connection parameter #646

Open vireshov opened 3 years ago

vireshov commented 3 years ago

Hi guys !

Background:

I'm currently writing a script to fetch VMs attached to specific VXLANs. This to create a VM batchlist for our VMware migration project.

I have multiple NSX-V Managers which I am connecting to and obviously also multiple vCenters.

My script is connecting to all of these NSX and vCenter instances and I need to perform actions on specific instances based on certain logic.

I've stored the instances as objects in an array to loop over them to do operations on multiple instances (based on specific logic).

Issue:

I have found that the "Get-NSXBackingPortGroup" can only be used when the "$global:DefaultVIServer" is set. This is not workable in certain situations. As I mentioned my script is connecting to multiple instances and it needs to do actions on specific vCenters based on certain logic.

Solution:

I have locally changed the PowerNSX module to workaround this "missing feature" of being able to specify a VIConnection object for this "Get-NSXBackingPortGroup" cmdlet.

We can now specify a "-Connection" argument, which will then be used in the "Get-VDPortgroup" cmdlet within the "Get-NSXBackingPortGroup" function:

function Get-NsxBackingPortGroup{

    <#
    .SYNOPSIS
    Gets the PortGroups backing an NSX Logical Switch.

    .DESCRIPTION
    NSX Logical switches are backed by one or more Virtual Distributed Switch
    portgroups that are the connection point in vCenter for VMs that connect to
    the logical switch.

    In simpler environments, a logical switch may only be backed by a single
    portgroup on a single Virtual Distributed Switch, but the scope of a logical
    switch is governed by the transport zone it is created in.  The transport
    zone may span multiple vSphere clusters that have hosts that belong to
    multiple different Virtual Distributed Switches and in this situation, a
    logical switch would be backed by a unique portgroup on each Virtual
    Distributed Switch.

    This cmdlet requires an active and correct PowerCLI connection to the
    vCenter server that is registered to NSX.  It returns PowerCLI VDPortgroup
    objects for each backing portgroup.

    .EXAMPLE

    #>

     param (

        [Parameter (Mandatory=$true,ValueFromPipeline=$true)]
            [ValidateScript({ValidateLogicalSwitch $_ })]
            [object]$LogicalSwitch,
        [Parameter (Mandatory=$False)]
            #PowerCLI Connection object
            [ValidateNotNullOrEmpty()]
            [PSCustomObject]$Connection=$global:DefaultVIServer
    )

    begin {

        if ( -not ( $Connection.IsConnected )) {
            throw "This cmdlet requires a valid PowerCLI connection.  Use Connect-VIServer to connect to vCenter and try again."
        }

}

    process {

        $BackingVDS = $_.vdsContextWithBacking
        foreach ( $vDS in $BackingVDS ) {

            write-debug "$($MyInvocation.MyCommand.Name) : Backing portgroup id $($vDS.backingValue)"

            try {
                Get-VDPortgroup -Server $Connection -Id "DistributedVirtualPortgroup-$($vDS.backingValue)"
            }
            catch {
                throw "VDPortgroup not found on connected vCenter $($Connection.Name).  $_"
            }
        }
    }

    end {}

}

As mentioned, I have implemented this locally now and it works fine. As at the moment I do not have much time to clone the repo, make the changes (and write a test), perhaps someone can review the above and then make the changes and pull request.

Thanks and Regards,

Viresh