PulsedHalo
PulsedHalo

Reputation: 11

List of file share for all storage accounts in Azure

I'd like to gather a list of all of the File Shares under every storage account we have in Azure. Most of the scripts i'm seeing show how to gather the file shares for an individual storage account. Is there a way to gather the info for all of them at once?

Upvotes: 1

Views: 5671

Answers (3)

Paul Dawson
Paul Dawson

Reputation: 1382

Here's a PowerShell script to output much the same as the bash script. The bash script uses a for loop in bash shell with the azure-cli. This one uses foreach loop in powershell with the azure-cli. Native powershell with the AZ commandlets doesn't seem to provide an easy way to do this (that I am aware of). Essentially both of these are using two tools to get the job done i.e. 1) Powershell or bash as the shell to loop and 2) azure-cli as the query method.

#Get storageaccount names
    $SAname=Get-AzStorageAccount

#Now iterate over the storageaccounts
    foreach ( $storageaccount in $SAname.StorageAccountName) 
    { write-output $storageaccount $(az storage share list --account-name $storageaccount --output tsv).replace("None","")}

It isn't pretty but it should give you an idea where to start.

Upvotes: 0

Charles Xu
Charles Xu

Reputation: 31384

To get the Azure File Share through PowerShell, you'd better use the Azure PowerShell instead of the Azure CLI, I think the CLI is not suitable for Linux. You can install the Azure PowerShell module and then use the script like this:

$storageAccount = Get-AzStorageAccount
foreach ($storage in $storageAccount) { 
    if($storage.PrimaryEndpoints.File -ne $null){
        Get-AzRmStorageShare -ResourceGroupName $storage.ResourceGroupName 
        }
}

Upvotes: 0

Paul Dawson
Paul Dawson

Reputation: 1382

Here's a quick bash script and one-liner using azure-cli 2.0.79 that might help you get started with iterating over all storage accounts under a single subscription.

From a bash script:

#!/bin/bash
#get a list of storage accounts
 for account in `az storage account list --query '[].{Name:name}' --output tsv` 
#iterate over storage accounts
 do 
  echo $account $(az storage share list --account-name $account --output tsv | awk '{print $2}')
done

One liner:

for account in `az storage account list --query '[].{Name:name}' --output tsv`;  do echo $account $(az storage share list --account-name $account --output tsv | awk '{print $2}') ; done

This should output:

storageaccountname1 <share1> <share2> <share3>
storageaccountname2 <share1> <share2>

Upvotes: 3

Related Questions