Not listing the files and folders inside the azure file share

47 Views Asked by At
$resourceGroupName = "xbackbonektadevmc-rg"
$storageAccName = "xfssaktamc601"
$fileShareName = "imagingcenter"
$directoryPath = "Software\KTA OPMT\KofaxTotalAgility-7.11 Software\KofaxTotalAgility-7.11.0.11_OPMT"

# Define the function to list directories and files recursively
Function GetFiles 
{ 
    param (
        [string]$path
    )

    Write-Host -ForegroundColor Green "Lists directories and files under $path.."   

    ## Get the storage account context 
    $ctx = $storageAccount.Context

    # Retrieve all files and folders under the file share
    $filesAndFolders = Get-AZStorageFile -Context $ctx -ShareName $fileShareName -Path $path

    ## Loop through files and folders 

    foreach($item in $filesAndFolders) 
    { 
        # Check if the item is a directory
        if ($item.GetType().Name -eq "AzureStorageFileDirectory")
        {
            Write-Host -ForegroundColor Red "Folder Name: " $item.Name 

            # Recursively call GetFiles function to list contents of subfolders
            if ($item.Path -like "$directoryPath\*") {
                GetFiles -path $item.Path
            }
        }
        else
        {
            # Display file name
            Write-Host -ForegroundColor Yellow "File Name: " $item.Name 
        }
    } 
} 

# Start listing files and folders from the KofaxTotalAgility-7.11.0.11_OPMT folder

GetFiles -path $directoryPath

I have tried executing the script it just display the name of the folder KofaxTotalAgility-7.11.0.11_OPMT but it is not listing the files and folders inside it can you please help me adjust this script

1

There are 1 best solutions below

0
Venkatesan On

Not listing the files and folders inside the Azure file share.

You can use the script below to list both folder and file names using Azure PowerShell.

Script:

$resourceGroupName = "<Your-resource-grp name>"
$storageAccountName = "your-storage-account-name"
$fileShareName = "<Your-file-share-name>"
$directoryPath = "files/sample1"

$storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName
$ctx = $storageAccount.Context

Function GetFiles 
{ 
    param (
        [string]$path
    )

    Write-Host -ForegroundColor Green "Lists directories and files under $path.."   
    $ctx = $storageAccount.Context

    $filesAndFolders = Get-AZStorageFile -Context $ctx -ShareName $fileShareName -Path $path |Get-AZStorageFile
    foreach($item in $filesAndFolders) 
    { 
        if ($item.GetType().Name -eq "AzureStorageFileDirectory")
        {
            Write-Host -ForegroundColor Red "Folder Name: " $item.Name 
        }
    }
    foreach($item in $filesAndFolders) 
    { 
        if ($item.GetType().Name -eq "AzureStorageFile")
        {
            Write-Host -ForegroundColor Yellow "File Name: " $item.Name 
        }
    }
} 

GetFiles -path $directoryPath

The script above lists all directories and files under a specified path in an Azure file share. It takes in the resource group name, storage account name, file share name, and directory path as parameters. The script retrieves the storage account context and uses the Get-AZStorageFile cmdlet to list the directories and files.

Output:

Lists directories and files under files/sample1..
Folder Name:  sample2
Folder Name:  sample3
File Name:  05-03-2024.html
File Name:  78089305.html
File Name:  78106201.html

enter image description here

Reference:

Get-AzStorageFile (Az.Storage) | Microsoft Learn