@@ -26,7 +26,113 @@ This script calculates the size of all Azure Blob Storage containers in a storag
26
26
27
27
## Sample script
28
28
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
+ ```
30
136
31
137
## Clean up deployment
32
138
0 commit comments