So I am trying to list the contents on an Azure blog container size, I can get how much storage is being used as a whole. What I trying to do is break it down, by the first position of the filename.
And this is my sad attempt. Could someone point me in the right direction please?
$ResourceGroup = "RG"
$StorageAccountName = "SAN"
$ContainerName = "CN"
$storageAccount = Get-AzStorageAccount `
-ResourceGroupName $ResourceGroup `
-Name $StorageAccountName
$Context = $storageAccount.Context
$Blobs = Get-AzStorageBlob -Container $ContainerName -Context $Context
$length = 0
$Blobs | ForEach-Object {$length = $length $_.Length }
#$Blobs.Name.Split("_",2)[0]
$Blobs | Select-Object Name, Length
$TotalSize = [math]::Round(($length / 1024 / 1024 / 1024 / 1024),2)
Write-Host "Total Size: $TotalSize Terabytes"
Current Output. ABCD_History_20221127_110045 9306112
ABCD_History_20221204_110052 11010048
ABCD_History_20221211_110045 10616832
EFGH_20220327_110201 48562176
EFGH_20220403_110159 46596096
Total Size: 29.63 Terabytes
Desired Output ABCD 30932992
EFGH 95158272
Total Size: 29.63 Terabytes
CodePudding user response:
I have reproduced in my environment and got expected results as below and I followed Microsoft-Document:
$ResourceGroup = "XX"
$StorageAccountName = "rith"
$ContainerName = "rithwik"
$storageAccount = Get-AzStorageAccount `
-ResourceGroupName $ResourceGroup `
-Name $StorageAccountName
$Context = $storageAccount.Context
$Blobs = Get-AzStorageBlob -Container $ContainerName -Context $Context
$length = 0
$Blobs | ForEach-Object {$length = $length $_.Length }
$Blobs | Select-Object Name, Length
After that used below code to get required Output:
foreach($emo in $blobs)
{
$BlobName=$Blobs.Name.Substring(0,3)
}
$Target = @()
foreach($emo in $BlobName)
{
$bn=$emo
$x=0
foreach($b in $blobs)
{
if ($b.Name -match $Bn)
{
$x=$x $b.Length
}
}
$out = $bn $x
$Target = $out
}
$Target | select -uniq
If you want 4 letters in output give 4 in substring
command instead of 3.
If you space between name and length you use like below in place of $out = $bn $x
in for loop:
$out = $bn " " $x