Public/Find-specMatchingGroupName.ps1

function Find-specMatchingGroupName {
    <#
.SYNOPSIS
    Finds a matching group name between two arrays.
 
.DESCRIPTION
    This function takes two arrays of Azure group names and identifies the single group name that exists in both arrays. It is designed to handle scenarios where only one match is expected.
 
.PARAMETER GroupArray1
    The first array of group names.
 
.PARAMETER GroupArray2
    The second array of group names.
 
.INPUTS
    System.String[]
 
.OUTPUTS
    System.String
 
.EXAMPLE
    $AzureGroupNames1 = @("GroupA", "GroupB", "GroupC")
    $AzureGroupNames2 = @("GroupX", "GroupB", "GroupY")
    Find-specMatchingGroupName -GroupArray1 $AzureGroupNames1 -GroupArray2 $AzureGroupNames2
 
    This example will output:
    GroupB
 
.NOTES
    This function uses a 'break' statement to exit the loop after finding the first match, as it assumes only one match exists.
 
    Author: owen.heaume
    Version: 1.0 -Initial release
#>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [string[]]$GroupArray1,

        [Parameter(Mandatory = $true)]
        [string[]]$GroupArray2
    )

    begin {
        $MatchingGroupName = $null
    }

    process {
        try {
            # Loop through the first array
            foreach ($GroupName in $GroupArray1) {
                # Check if the current group name exists in the second array
                if ($GroupArray2 -contains $GroupName) {
                    # If a match is found, assign it to the variable and exit the loop
                    $MatchingGroupName = $GroupName
                    break
                }
            }
        } catch {
            Write-Error "An error occurred: $_"
        }
    }

    end {
        # Output the matching group name
        if ($MatchingGroupName) {
            return $MatchingGroupName
        } else {
            Write-Warning 'No matching group name found.'
        }
    }
}