Scripts/Get-NFSv3UsersOfExtendedGroups.ps1

<#
    .SYNOPSIS
    Lists NFSv3 users part of all extended groups stored in Azure DB.
 
    .DESCRIPTION
    This command lists NFSv3 users stored in Azure DB for all the Gids. If Gid is provided, it will just list the users part of that Gid.
 
    .PARAMETER ResourceGroupName
    The name of the Resource Group in Azure where the storage account resides.
 
    .PARAMETER StorageAccountName
    The name of the Azure storage account.
 
    .PARAMETER Gid
    Optional, Uids of all users part of this Gid will be listed.
 
    .EXAMPLE
    Get-NFSv3UsersOfExtendedGroups -ResourceGroupName "MyRG" -StorageAccountName "MyStorage" -Gid "1001"
 
    .NOTES
    Written by: [Azure Blob NFS]
    Date: [October 10, 2024]
#>

function Get-NFSv3UsersOfExtendedGroups{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        [string]$ResourceGroupName,
        
        [Parameter(Mandatory=$true)]
        [string]$StorageAccountName,
        
        [Parameter(Mandatory=$false)]
        [string]$Gid
    )

    $prefix = "nfsv3_"
    
    # Get all the NFSv3 localusers because irrespective of Gid is provided, we will need it.
    $localUsers = Get-AzStorageLocalUser -ResourceGroupName $ResourceGroupName -StorageAccountName $StorageAccountName -IncludeNFSv3

    if ($PSBoundParameters.ContainsKey('Gid')) {
        if ([string]::IsNullOrEmpty($Gid)) {
            Write-NFSv3ExtendedGroupsLog -Message "Gid can not be null or empty." -LogLevel "ERROR"
            return
        }
        
        # Array to store all the uids part of this Gid.
        $uids = @()

        foreach ($localUser in $localUsers) {
            # Get the Uid from localUser name. LocalUser name is in format "nfsv3_<uid>".
            $uid = $localUser.Name -replace "^$prefix", ""
            $extendedGroups = $localUser.ExtendedGroups

            foreach ($groupId in $extendedGroups) {
                if ($groupId -eq $Gid) {
                    $uids += $uid
                    break
                }
            }
        }

        # Sort array before printing.
        $uids = $uids | Sort-Object

        # Print all the uids part of this Gid as comma separated values.
        $uids -join ","
    } else {
        # Create a hash map to store all the uids part of specific group gid.
        $gidToUidNumberMap = @{}

        foreach ($localUser in $localUsers) {
            # Get the Uid from localUser name. LocalUser name is in format "nfsv3_<uid>".
            $uid = $localUser.Name -replace "^$prefix", ""
            $extendedGroups = $localUser.ExtendedGroups

            foreach ($groupId in $extendedGroups) {
                if (-not $gidToUidNumberMap.ContainsKey($groupId)) {
                    $gidToUidNumberMap[$groupId] = @()
                }

                $gidToUidNumberMap[$groupId] += $uid
            }
        }

        # Sort hash map based on key before printing.
        $gidToUidNumberMap = $gidToUidNumberMap.GetEnumerator() | Sort-Object Name

        $gidToUidNumberMap.GetEnumerator() | Select-Object @{
            Name = 'Gid'
            Expression = { $_.key }
        }, @{
            Name = 'Users'
            Expression = { $_.Value -join ", " }
        } | Format-Table -AutoSize
    }
}