-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetContainerUsage.ps1
140 lines (108 loc) · 4.45 KB
/
GetContainerUsage.ps1
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
###
## DISCLAIMER : This is a sample and is provided as is with no warranties express or implied.
##
###
[CmdletBinding(DefaultParametersetName="SharedKey")]
param(
[Parameter(Mandatory=$true, HelpMessage="Storage Account Name")]
[String] $storage_account_name,
[Parameter(Mandatory=$true, HelpMessage="Any one of the two shared access keys", ParameterSetName="SharedKey", Position=1)]
[String] $storage_shared_key,
[Parameter(Mandatory=$true, HelpMessage="SAS Token : the GET parameters", ParameterSetName="SASToken", Position=1)]
[String] $storage_sas_token,
[Parameter(Mandatory=$true, HelpMessage="Container Name", Position=2)]
[String] $container_name,
[Parameter(Mandatory=$false, HelpMessage="Folder Name", Position=3)]
[String] $folder_name
)
$containerstats = @()
$output = "c:\output"
$outfile = "c:\output\log.txt"
If ($PsCmdlet.ParameterSetName -eq "SharedKey")
{
$Ctx = New-AzStorageContext -StorageAccountName $storage_account_name -StorageAccountKey $storage_shared_key
}
Else
{
$Ctx = New-AzStorageContext -StorageAccountName $storage_account_name -SasToken $storage_sas_token
}
$container_continuation_token = $null
do {
$containers = Get-AzStorageContainer -Context $Ctx -MaxCount 1 -ContinuationToken $container_continuation_token
$container_continuation_token = $null;
if ($containers -ne $null)
{
$container_continuation_token = $containers[$containers.Count - 1].ContinuationToken
for ([int] $c = 0; $c -lt $containers.Count; $c++)
{
$container = $containers[$c].Name
if (($container_name -ne $null) -and (-not ($container_name -like $container)))
{
continue
}
Write-Host "Processing container : $container"
$total_usage = 0
$total_blob_count = 0
$soft_delete_usage = 0
$soft_delete_count = 0
$blob_continuation_token = $null
$filestats = @()
do {
if ($folder_name -eq $null)
{
$blobs = Get-AzStorageBlob -Context $Ctx -Container $container -MaxCount 5000 -IncludeDeleted -ContinuationToken $blob_continuation_token
}
else {
$blobs = Get-AzStorageBlob -Context $Ctx -Container $container -MaxCount 5000 -IncludeDeleted -ContinuationToken $blob_continuation_token -Prefix $folder_name
}
$blob_continuation_token = $null;
if ($blobs -ne $null)
{
$blob_continuation_token = $blobs[$blobs.Count - 1].ContinuationToken
for ([int] $b = 0; $b -lt $blobs.Count; $b++)
{
$total_blob_count++
$total_usage += $blobs[$b].Length
if ($blobs[$b].IsDeleted)
{
$soft_delete_count++
$soft_delete_usage += $blobs[$b].Length
}
$logline = $blobs[$b].Name + "|" + $blobs[$b].Length + "|" + $blobs[$b].LastModified
$logline | out-file -Filepath $outfile -append
$filestats += [PSCustomObject] @{
Name = $blobs[$b].Name
Length = $blobs[$b].Length
LastModified = $blobs[$b].LastModified
}
}
#$filestats | Format-Table -AutoSize
#log $filestats
If ($blob_continuation_token -ne $null)
{
Write-Verbose "Blob listing continuation token = {0}".Replace("{0}",$blob_continuation_token.NextMarker)
}
}
} while ($blob_continuation_token -ne $null)
Write-Verbose "Calculated size of $container = $total_usage with soft_delete usage of $soft_delete_usage"
$containerstats += [PSCustomObject] @{
Name = $container
TotalBlobCount = $total_blob_count
TotalBlobUsage = $total_usage
SoftDeletedBlobCount = $soft_delete_count
SoftDeletedBlobUsage = $soft_delete_usage
}
$folder_name2 = $folder_name.Replace("/", "_")
$outputPath = $output + "\BlobFiles_" + $folder_name2 + ".csv"
$filestats | Export-Csv -Path $outputPath
$outputPath2 = $output + "\PathFiles_" + $folder_name2 + ".csv"
$containerstats | Export-Csv -Path $outputPath2
}
}
If ($container_continuation_token -ne $null)
{
Write-Verbose "Container listing continuation token = {0}".Replace("{0}",$container_continuation_token.NextMarker)
}
} while ($container_continuation_token -ne $null)
Write-Host "Total container stats"
$containerstats | Format-Table -AutoSize