-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscriptname.ps1
More file actions
48 lines (38 loc) · 1.42 KB
/
scriptname.ps1
File metadata and controls
48 lines (38 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
$resourceGroup = "xyz-rg"
$storageAccountName = "xyz-sa"
$containerName = "xyz-cont"
$storageAccount = Get-AzStorageAccount `
-ResourceGroupName $resourceGroup `
-Name $storageAccountName
$ctx = $storageAccount.Context
# get a list of all of the blobs in the container
$listOfBlobs = Get-AzStorageBlob -Container $containerName -Context $ctx
# create a dictionary to store the size of each folder and initialize it with 0 values
$folderSizes = @{}
foreach ($blob in $listOfBlobs) {
$folder = ($blob.Name -split '/')[0]
if (-not $folderSizes.ContainsKey($folder)) {
$folderSizes[$folder] = 0
}
$folderSizes[$folder] += $blob.Length
}
# create a new object for each folder with its name and size in MB
$folderObjects = foreach ($key in $folderSizes.Keys) {
[PSCustomObject]@{
'Folder Name' = $key
'Size (MB)' = [math]::Round(($folderSizes[$key] / 1MB), 2)
}
}
# calculate the total size of all folders
$totalSize = [math]::Round(($folderSizes.Values | Measure-Object -Sum).Sum / 1MB, 2)
# create a new object for the total size
$totalObject = [PSCustomObject]@{
'Folder Name' = 'Total'
'Size (MB)' = $totalSize
}
# combine the folder objects and the total object
$results = $folderObjects + $totalObject
# output the results to a CSV file
$results | Export-Csv -Path 'folder_sizes.csv' -NoTypeInformation -Encoding UTF8
# display the results in the console
$results | Format-Table