Scripts/GetNFSv3UsersOfExtendedGroups.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
    )

    # logMessage is written in this way to keep the indentation correct in log file.
    $logMessage = @"
Get-NFSv3UsersOfExtendedGroups started,
        ResourceGroupName : $ResourceGroupName,
        StorageAccountName : $StorageAccountName
"@

    
    if ($Gid) { $logMessage +=  ",`n Gid : $Gid" }
  
    Write-NFSv3ExtendedGroupsLog $logMessage -VerbosePreference $VerbosePreference
    
    $prefix = "nfsv3_"
    
    # Get all the NFSv3 localusers because irrespective of Gid is provided, we will need it.
    try {
        $localUsers = Get-AzStorageLocalUser -ResourceGroupName $ResourceGroupName -StorageAccountName $StorageAccountName -IncludeNFSv3 -ErrorAction Stop
        Write-NFSv3ExtendedGroupsLog "Fetched $($localUsers.Count) LocalUsers from Azure Storage server" -VerbosePreference $VerbosePreference 
    } catch {
        # Kill the script if Get-AzStorageLocalUser fails.
        Write-NFSv3ExtendedGroupsLog "Get-AzStorageLocalUser failed with error: $_" -LogLevel "ERROR"
        return
    }

    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 below.
        #
        # Gid Users
        # --- -----
        # 1000 1001, 1002, 1003, 1004, 1005
        #
        [pscustomobject]@{
            Gid = $Gid
            Users = $uids -join ", "
        } | Format-Table -AutoSize -Wrap
    } 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

        #
        # Print all the uids part of each Gid from map as below.
        #
        # Gid Users
        # --- -----
        # 1000 1001, 1002, 1003, 1004, 1005
        # 2000 2001, 2002, 2003, 2004, 2005
        #
        $gidToUidNumberMap.GetEnumerator() | Select-Object @{
            Name = 'Gid'
            Expression = { $_.key }
        }, @{
            Name = 'Users'
            Expression = { $_.Value -join ", " }
        } | Format-Table -AutoSize -Wrap
    }

    Write-NFSv3ExtendedGroupsLog "Get-NFSv3UsersOfExtendedGroups completed" -VerbosePreference $VerbosePreference
}