Skip to content

Commit 1d9ff01

Browse files
authored
Merge pull request #259212 from normesta/scripts
Updating script in article
2 parents 1f67729 + 9334eed commit 1d9ff01

File tree

1 file changed

+116
-9
lines changed

1 file changed

+116
-9
lines changed

articles/storage/scripts/storage-blobs-container-calculate-size-powershell.md

Lines changed: 116 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,138 @@
11
---
2-
title: Calculate size of a blob container with PowerShell
2+
title: Calculate the size of blob containers with PowerShell
33
titleSuffix: Azure Storage
4-
description: Calculate the size of a container in Azure Blob Storage by totaling the size of each of its blobs.
4+
description: Calculate the size of all Azure Blob Storage containers in a storage account.
55
services: storage
6-
author: stevenmatthew
6+
author: normesta
77

88
ms.service: azure-storage
99
ms.devlang: powershell
1010
ms.topic: sample
11-
ms.date: 12/04/2019
12-
ms.author: shaas
11+
ms.date: 11/21/2023
12+
ms.author: normesta
1313
ms.custom: devx-track-azurepowershell
1414
---
1515

16-
# Calculate the size of a blob container with PowerShell
16+
# Calculate the size of blob containers with PowerShell
1717

18-
This script calculates the size of a container in Azure Blob Storage. It first displays the total number of bytes used by the blobs within the container, then displays their individual names and lengths.
18+
This script calculates the size of all Azure Blob Storage containers in a storage account.
1919

2020
[!INCLUDE [sample-powershell-install](../../../includes/sample-powershell-install-no-ssh-az.md)]
2121

2222
[!INCLUDE [quickstarts-free-trial-note](../../../includes/quickstarts-free-trial-note.md)]
2323

2424
> [!IMPORTANT]
25-
> This PowerShell script provides an estimated size for the container and should not be used for billing calculations. For a script that calculates container size for billing purposes, see [Calculate the size of a Blob storage container for billing purposes](../scripts/storage-blobs-container-calculate-billing-size-powershell.md).
25+
> This PowerShell script provides an estimated size for the containers in an account and should not be used for billing calculations. For a script that calculates container size for billing purposes, see [Calculate the size of a Blob storage container for billing purposes](../scripts/storage-blobs-container-calculate-billing-size-powershell.md).
2626
2727
## Sample script
2828

29-
[!code-powershell[main](../../../powershell_scripts/storage/calculate-container-size/calculate-container-size.ps1 "Calculate container size")]
29+
```powershell
30+
# This script will show how to get the total size of the blobs in all containers in a storage account.
31+
# Before running this, you need to create a storage account, at least one container,
32+
# and upload some blobs into that container.
33+
# note: this retrieves all of the blobs in each container in one command.
34+
# Run the Connect-AzAccount cmdlet to connect to Azure.
35+
# Requests that are sent as part of this tool will incur transactional costs.
36+
#
37+
38+
$containerstats = @()
39+
40+
# Provide the name of your storage account and resource group
41+
$storage_account_name = "<name-of-your-storage-account>"
42+
$resource_group = "<name-of-your-resource-group"
43+
44+
# Get a reference to the storage account and the context.
45+
$storageAccount = Get-AzStorageAccount `
46+
-ResourceGroupName $resource_group `
47+
-Name $storage_account_name
48+
$Ctx = $storageAccount.Context
49+
50+
$container_continuation_token = $null
51+
do {
52+
$containers = Get-AzStorageContainer -Context $Ctx -MaxCount 5000 -ContinuationToken $container_continuation_token
53+
$container_continuation_token = $null;
54+
55+
if ($containers -ne $null)
56+
{
57+
$container_continuation_token = $containers[$containers.Count - 1].ContinuationToken
58+
59+
for ([int] $c = 0; $c -lt $containers.Count; $c++)
60+
{
61+
$container = $containers[$c].Name
62+
Write-Verbose "Processing container : $container"
63+
$total_usage = 0
64+
$total_blob_count = 0
65+
$soft_delete_usage = 0
66+
$soft_delete_count = 0
67+
$version_usage = 0
68+
$version_count =
69+
$snapshot_count = 0
70+
$snapshot_usage = 0
71+
$blob_continuation_token = $null
72+
73+
do {
74+
$blobs = Get-AzStorageBlob -Context $Ctx -IncludeDeleted -IncludeVersion -Container $container -ConcurrentTaskCount 100 -MaxCount 5000 -ContinuationToken $blob_continuation_token
75+
$blob_continuation_token = $null;
76+
77+
if ($blobs -ne $null)
78+
{
79+
$blob_continuation_token = $blobs[$blobs.Count - 1].ContinuationToken
80+
81+
for ([int] $b = 0; $b -lt $blobs.Count; $b++)
82+
{
83+
$total_blob_count++
84+
$total_usage += $blobs[$b].Length
85+
86+
if ($blobs[$b].IsDeleted)
87+
{
88+
$soft_delete_count++
89+
$soft_delete_usage += $blobs[$b].Length
90+
}
91+
92+
if ($blobs[$b].SnapshotTime -ne $null)
93+
{
94+
$snapshot_count++
95+
$snapshot_usage+= $blobs[$b].Length
96+
}
97+
98+
if ($blobs[$b].VersionId -ne $null)
99+
{
100+
$version_count++
101+
$version_usage += $blobs[$b].Length
102+
}
103+
}
104+
105+
If ($blob_continuation_token -ne $null)
106+
{
107+
Write-Verbose "Blob listing continuation token = {0}".Replace("{0}",$blob_continuation_token.NextMarker)
108+
}
109+
}
110+
} while ($blob_continuation_token -ne $null)
111+
112+
Write-Verbose "Calculated size of $container = $total_usage with soft_delete usage of $soft_delete_usage"
113+
$containerstats += [PSCustomObject] @{
114+
Name = $container
115+
TotalBlobCount = $total_blob_count
116+
TotalBlobUsageinGB = $total_usage/1GB
117+
SoftDeletedBlobCount = $soft_delete_count
118+
SoftDeletedBlobUsageinGB = $soft_delete_usage/1GB
119+
SnapshotCount = $snapshot_count
120+
SnapshotUsageinGB = $snapshot_usage/1GB
121+
VersionCount = $version_count
122+
VersionUsageinGB = $version_usage/1GB
123+
}
124+
}
125+
}
126+
127+
If ($container_continuation_token -ne $null)
128+
{
129+
Write-Verbose "Container listing continuation token = {0}".Replace("{0}",$container_continuation_token.NextMarker)
130+
}
131+
} while ($container_continuation_token -ne $null)
132+
133+
Write-Host "Total container stats"
134+
$containerstats | Format-Table -AutoSize
135+
```
30136

31137
## Clean up deployment
32138

@@ -43,6 +149,7 @@ This script uses the following commands to calculate the size of the Blob storag
43149
| Command | Notes |
44150
|---|---|
45151
| [Get-AzStorageAccount](/powershell/module/az.storage/get-azstorageaccount) | Gets a specified Storage account or all of the Storage accounts in a resource group or the subscription. |
152+
| [Get-AzStorageContainer](/powershell/module/az.storage/get-azstoragecontainer) | Lists the storage containers. |
46153
| [Get-AzStorageBlob](/powershell/module/az.storage/Get-AzStorageBlob) | Lists blobs in a container. |
47154

48155
## Next steps

0 commit comments

Comments
 (0)