SearchAzureStorageForFiles.ps1


<#PSScriptInfo
 
.VERSION 1.0.0
 
.GUID d300bcc6-e3a8-406e-9224-452202bd4738
 
.AUTHOR jadedarchitect
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS Azure Storage Blob File Search Azure Powershell
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
.PRIVATEDATA
 
#>
 

#Requires -Module Az


<#
 
.DESCRIPTION
Searches across all resource groups, storage accounts, and containers in the specified subscription for blobs matching a name or partial name
 
#>
 

Param()


#Sign in using identity that has access to ALL resources (Or the ones you need) in the subscription you need to search!
Connect-AzAccount

#Set your subscription ID unless you want to have a bad day.
#Find it using https://portal.azure.com/#blade/Microsoft_Azure_Billing/SubscriptionsBlade
$Subscription = read-host -prompt "Input subscription ID. Find it at https://portal.azure.com/#blade/Microsoft_Azure_Billing/SubscriptionsBlade"
$Context = (Set-AzContext -Subscription $Subscription)

#Specify the name of the file you want to look for. Be as specific as possible!
$FileName = read-host -prompt "Specify the file name to search for (Accepts partial file names)"

#List resource groups
$ResourceGroups = (Get-AzResourceGroup | select ResourceGroupName)
foreach ($Res in $ResourceGroups) {
    $Res1 = $Res.ResourceGroupName
    write-host "Looking at Resource Group $Res1"
    start-sleep -seconds 2

    #List storage accounts in each resource group
    $Names = (Get-AzStorageAccount -ResourceGroupName $Res1 | Select Context, StorageAccountName)
    foreach ($Name in $Names) {
        $CTX = $Name.Context
        $Name1 = $Name.StorageAccountName
        write-host "Looking at Storage account $Name1, Storage Account of $Res1"
        start-sleep -seconds 2

        #Get the key for the storage account
        $Keylist = (Get-AzStorageAccountKey -ResourceGroupName $Res1 -AccountName $Name1 | Select Value)
        foreach ($Key in $Keylist) {

            #Connect using key
            $Keyvalue = $Key.Value
            write-host "Key value for Storage Account $Name1 is $Keyvalue in resource group $Res1 . Locating containers and searching for storage blobs"
            start-sleep -seconds 1

            #List containers in each storage account
            $Containers = (Get-AzStorageContainer -Context $ctx)
            foreach ($Container in $Containers) {
                $Cname = $Container.name
                write-host "Looking at container $Cname in Resource Group $Name1 for storage blobs."
                start-sleep -seconds 1

                #List storage blobs in each container
                $Blobs = (Get-AzStorageBlob -Container $Cname -Context $ctx)
                write-host "Searching all blobs in container $Cname for files with $FileName in the name"
                start-sleep -seconds 1
                #Go silent, look for filenames.
                foreach ($Blob in $Blobs) {
                    $Bname = $Blob.Name
                    if ($Bname -like "*$FileName*") {
                        #This matches the substring
                        write-host "$FileName located in container $Cname , Storage account $Name1 in resource group $Res1" -foregroundcolor red -BackgroundColor yellow
                        Write-Warning "File located. Do you wish to continue searching? Hit Yes to All to ignore these warnings." -WarningAction Inquire
                    }
                    else { continue }
                    start-sleep -seconds 2
                }
            }
        }
    }
}