Export Azure File Shares files to CSV

Just like it sucks to search directories and file names, it sucks to output files and their sizes.

Note: You'll need Subscription ID, Resource Group Name, Storage Account Name, and Azure Share Name

# Set mandatory parameters
param 
(
    [Parameter(Mandatory=$true)]
    [string] $subscriptionId,
    [Parameter(Mandatory=$true)]
    [string] $resourceGroupName,
    [Parameter(Mandatory=$true)]
    [string] $storageAccountName,
    [Parameter(Mandatory=$true)]
    [string] $storageShareName
)
# Repeat Loop start
Do {
Connect-AzAccount
# Set Azure context
$context = Set-AzContext -SubscriptionId $subscriptionId
# Get Storage Context
Write-Host "Searching for files in $storageShareName in $storageAccountName :" -ForegroundColor Green
Write-Host "=================================================================" -ForegroundColor Green
$key = Get-AzStorageAccountkey -ResourceGroupName $resourceGroupName -Name $storageAccountName
$storContext = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $key.value[0]
$getAzSF = Get-AzStorageFile -ShareName $storageShareName -context $storContext
# Iterate through directories, find files and export Dir, Name and Length
	$(foreach ($dir in $getAzSF)
		{
			Get-AzStorageFile -ShareName $storageShareName -context $storContext -Path $($dir.name) | Get-AzStorageFile | Select-Object -Property @{L='Directory';E={$($dir.name)}}, Name, Length
		})| Export-Csv -Path .\$storageShareName.csv
    Write-host "Data exported to CSV!"
    Write-host ""
$repeat = Read-Host "Repeat?"
}
While ($repeat -eq "Y")
# Repeat Loop end
Write-Host ""
Write-Host "EXITING... " -ForegroundColor Yellow -BackgroundColor Black
Write-Host ""
# Disconnect
Disconnect-AzAccount > $null
Write-Host "ACCOUNT HAS BEEN DISCONNECTED" -ForegroundColor Yellow -BackgroundColor Black
# end

Leave a comment