Villar
Villar

Reputation: 71

Fast way to check azure storage account file share used capacity with powershell

It is too slow to check storage account file share folder number of files and capacity through Storage Explorer. Is there a more fast way to get this information? Through powershell, for example?

Upvotes: 1

Views: 5886

Answers (2)

Ivan Glasenberg
Ivan Glasenberg

Reputation: 30025

Please use the code below(write in PowerShell ISE):

$fileinfor =@{count=0;length=0}

function file_info()
{

$context = New-AzStorageContext -StorageAccountName your_storage_account_name -StorageAccountKey your_storage_account_key
$shares = Get-AzStorageShare -Context $context

foreach($share in $shares)
{
#get all the files and directories in a file share
$filesAndDirs = Get-AzStorageFile -ShareName $share.name -Context $context

foreach($f in $filesAndDirs)
{
if($f.gettype().name -eq "CloudFile")
{
Write-Output $f.name
$fileinfor["count"]++
$fileinfor["length"]=$fileinfor["length"]+$f.Properties.Length

}
elseif($f.gettype().name -eq "CloudFileDirectory")
{
list_subdir($f)
}
}

}
Write-Output ""
Write-Output "File total count: "$fileinfor["count"]
Write-Output "File total length: "$fileinfor["length"]

}


function list_subdir([Microsoft.WindowsAzure.Storage.File.CloudFileDirectory]$dirs)
{

$path = $dirs.Uri.PathAndQuery.Remove(0,($dirs.Uri.PathAndQuery.IndexOf('/',1)+1))
$filesAndDirs = Get-AzStorageFile -ShareName $dirs.share.name -Path $path -Context $context | Get-AzStorageFile
foreach($f in $filesAndDirs)
{
if($f.gettype().name -eq "CloudFile")
{
Write-Output $f.name
$fileinfor["count"]++
$fileinfor["length"]=$fileinfor["length"]+$f.Properties.Length

}
elseif($f.gettype().name -eq "CloudFileDirectory")
{
list_subdir($f)
}

}

}



file_info

And the test result:

enter image description here

Upvotes: 2

Sandy
Sandy

Reputation: 91

The below powershell code should work

$ctx = New-AzureStorageContext -StorageAccountName <storage-account-name> -StorageAccountKey <storage-account-key>
(Get-AzureStorageShare -Context $ctx).count

Upvotes: 0

Related Questions