Private/Resolve-IRODGroup.ps1

function Resolve-IRODGroup {
    <#
    .SYNOPSIS
        Resolves a group Object ID or display name to a single Entra ID group.
    .DESCRIPTION
        If the input is a GUID, the group is looked up by Object ID.
        Otherwise the input is treated as a display name: an exact
        (case-insensitive) match is tried first, then a "starts with" search.
        If more than one group matches, the user is asked to pick one.
        Writes the reason to the console and returns $null if no group is
        found or the user cancels.
    #>

    param(
        [Parameter(Mandatory)]
        [string]$GroupInput
    )

    $GroupInput = $GroupInput.Trim()

    # Object ID
    $parsedGuid = [guid]::Empty
    if ([guid]::TryParse($GroupInput, [ref]$parsedGuid)) {
        $group = Get-GroupById -GroupId $parsedGuid.ToString()
        if (-not $group) {
            Write-Host "`nGroup not found with Object ID: $GroupInput" -ForegroundColor Red
            Write-Host " Please verify the Object ID is correct and you have access to the group." -ForegroundColor Gray
        }
        return $group
    }

    # Display name - exact match first
    $maxResults = 25
    $candidates = @(Get-GroupByName -Name $GroupInput -Top $maxResults)
    $isExactMatch = $true

    if ($candidates.Count -eq 1) {
        return $candidates[0]
    }

    # No exact match - fall back to groups whose name starts with the input
    if ($candidates.Count -eq 0) {
        $isExactMatch = $false
        $candidates = @(Get-GroupByName -Name $GroupInput -StartsWith -Top $maxResults)
    }

    if ($candidates.Count -eq 0) {
        Write-Host "`nNo group found with name: $GroupInput" -ForegroundColor Red
        Write-Host " Please verify the group name (or use the Object ID) and that you have access to the group." -ForegroundColor Gray
        return $null
    }

    # Multiple exact matches, or partial matches that need confirmation - let the user pick
    Write-Host ""
    if ($isExactMatch) {
        Write-Host "Multiple groups are named '$GroupInput':" -ForegroundColor Yellow
    }
    else {
        Write-Host "No exact match for '$GroupInput'. Groups starting with that name:" -ForegroundColor Yellow
    }
    Write-Host ""

    for ($i = 0; $i -lt $candidates.Count; $i++) {
        $candidate = $candidates[$i]
        Write-Host (" [{0}] " -f ($i + 1)) -ForegroundColor Green -NoNewline
        Write-Host $candidate.displayName -ForegroundColor White -NoNewline
        Write-Host " ($($candidate.GroupType))" -ForegroundColor DarkGray
        Write-Host " $($candidate.id)" -ForegroundColor DarkGray
    }

    if ($candidates.Count -ge $maxResults) {
        Write-Host ""
        Write-Host " Showing the first $maxResults matches. Enter a more specific name or the Object ID to narrow results." -ForegroundColor Gray
    }

    Write-Host ""
    do {
        $selection = Read-Host "Select a group (1-$($candidates.Count)), or Q to cancel"
        if ($selection -match '^\s*[Qq]\s*$') {
            Write-Host "`nGroup selection cancelled." -ForegroundColor Red
            return $null
        }

        $index = 0
        $isValid = [int]::TryParse($selection, [ref]$index) -and $index -ge 1 -and $index -le $candidates.Count
        if (-not $isValid) {
            Write-Host " Invalid selection." -ForegroundColor Red
        }
    } while (-not $isValid)

    return $candidates[$index - 1]
}