Searching Azure File Share to match string

It sucks. There's a search directory box in Explorer, so wtf Azure?! Can't search by anything except full directory name even with wildcards. Until now. Search for Directories and Files that match your string.

Note: You'll need Subscription ID, Resource Group Name, and Storage Account name of where you want to search.

# Set mandatory parameters
param 
(
    [Parameter(Mandatory=$true)]
    [string] $subscriptionId,
    [Parameter(Mandatory=$true)]
    [string] $resourceGroupName,
    [Parameter(Mandatory=$true)]
    [string] $storageAccountName
)
# Repeat Loop start
Do {
Connect-AzAccount
# Set Azure context
$context = Set-AzContext -SubscriptionId $subscriptionId
# Search string
$searchString = Read-Host -Prompt "Please enter string to search for"
# Get Storage Context
Write-Host "Searching Directories for $searchString in $storageAccountName :" -ForegroundColor Green
Write-Host "====================================================" -ForegroundColor Green
$key = Get-AzStorageAccountkey -ResourceGroupName $resourceGroupName -Name $storageAccountName
$storContext = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $key.value[0]
# Get Share Names
$shareNames = Get-AzRmStorageShare -ResourceGroupName $resourceGroupName -StorageAccountName $storageAccountName
foreach ($sName in $shareNames)
{
        $getAzSF = Get-AzStorageFile -ShareName $sName.name -context $storContext
# Iterate through directories, find Dir matches
        foreach ($dir in $getAzSF)
		{
			if ($dir.name -match $searchString)
			{
				Write-host "Path:  $($dir.name) " -ForegroundColor Yellow
		    }
        }
Write-Host "Searching Files for $searchString in $storageAccountName :" -ForegroundColor Green
Write-Host "====================================================" -ForegroundColor Green
# Iterate through directories, find File matches
foreach ($dir in $getAzSF)
		{
			$fileName = Get-AzStorageFile -ShareName $sName.name -context $storContext -Path $($dir.name) | Get-AzStorageFile | Select-Object -Property @{L='Directory';E={$($dir.name)}}, Name, Length
            if ($filename.name -match $searchString)
                {
                    Write-host "Path:  $($dir.name) " -ForegroundColor Yellow
                    Write-host "File Name: $($filename.name) " -ForegroundColor Yellow
                }
    }
$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 | Out-Null
Write-Host "ACCOUNT HAS BEEN DISCONNECTED" -ForegroundColor Yellow -BackgroundColor Black
# end

Leave a comment