Scripts/GetNFSv3ExtendedGroups.ps1

<#
    .SYNOPSIS
    Lists NFSv3 extended groups stored in Azure DB.
 
    .DESCRIPTION
    This command lists NFSv3 extended groups stored in Azure DB for all the Uids. If Uid is provided, it will just list the groups for that Uid.
 
    .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 Uid
    Optional, Uid of user whose supplementary groups needs to listed.
 
    .EXAMPLE
    Get-NFSv3ExtendedGroups -ResourceGroupName "MyRG" -StorageAccountName "MyStorage" -Uid "1001"
 
    .NOTES
    Written by: [Azure Blob NFS]
    Date: [October 10, 2024]
#>

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

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

    
    if ($Uid) { $logMessage +=  ",`n Uid : $Uid" }
  
    Write-NFSv3ExtendedGroupsLog $logMessage -VerbosePreference $VerbosePreference
    
    $prefix = "nfsv3_"

    if ($PSBoundParameters.ContainsKey('Uid')) {
        if ([string]::IsNullOrEmpty($Uid)) {
            Write-NFSv3ExtendedGroupsLog -Message "Uid can not be null or empty." -LogLevel "ERROR"
            return
        }
        
        # Get specific user if $Uid is provided.
        $userName = $prefix + $Uid

        # Get localuser with given UID from storage server.
        try {
            $localUser = Get-AzStorageLocalUser -ResourceGroupName $ResourceGroupName -StorageAccountName $StorageAccountName -UserName $userName -ErrorAction Stop
            Write-NFSv3ExtendedGroupsLog "Fetched $($localUser.ExtendedGroups.Count) groups for Uid $Uid from Azure Storage server" -VerbosePreference $VerbosePreference 
        } catch {
            # Kill the script if Get-AzStorageLocalUser fails.
            Write-NFSv3ExtendedGroupsLog "Get-AzStorageLocalUser failed with error: $_" -LogLevel "ERROR"
            return
        }

        #
        # Print all the gids part of this Uid as below.
        #
        # Uid Groups
        # --- -----
        # 1000 1001, 1002, 1003, 1004, 1005
        #
        [pscustomobject]@{
            Uid = $Uid
            Groups = $localUser.ExtendedGroups -join ", "
        } | Format-Table -AutoSize -Wrap
    } else {
        #
        # List all the users if $Uid is not provided.
        # Get all the LocalUsers from storage server for the given account.
        #
        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
        }

        #
        # Print all the gids part of each Uid from map as below.
        #
        # Uid Groups
        # --- -----
        # 1000 1001, 1002, 1003, 1004, 1005
        # 2000 2001, 2002, 2003, 2004, 2005
        #
        $localUsers | Select-Object @{
            Name = 'Uid'
            Expression = { $_.Name -replace "^$prefix", "" }
        }, @{
            Name = 'ExtendedGroups'
            Expression = { $_.ExtendedGroups -join ", " }
        } | Format-Table -AutoSize -Wrap
    }

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