|
27 | 27 |
|
28 | 28 | #Initialise meter hashtable |
29 | 29 | $meters = @{ |
| 30 | + '578ae51d-4ef9-42f9-85ae-42b52d3d83ac' = 'ActualPremiumSnapshotSize Unit: GB*month' |
| 31 | + '108fa95b-be0d-4cd9-96e8-5b0d59505df1' = 'ActualStandardSnapshotSize Unit: GB*month' |
| 32 | + 'Custom Worker Tiers' = 'Custom Worker Tiers Unit: Hours' |
30 | 33 | 'F271A8A388C44D93956A063E1D2FA80B' = 'Static IP Address Usage' |
31 | 34 | '9E2739BA86744796B465F64674B822BA' = 'Dynamic IP Address Usage' |
32 | 35 | 'B4438D5D-453B-4EE1-B42A-DC72E377F1E4' = 'TableCapacity' |
@@ -111,46 +114,51 @@ $meters = @{ |
111 | 114 | } |
112 | 115 |
|
113 | 116 | #Build a subscription hashtable |
114 | | -$subtable = @{} |
| 117 | +$subtable = New-Object System.Collections.ArrayList |
115 | 118 | $subs = Get-AzsUserSubscription |
116 | | -$subs | ForEach-Object {$subtable.Add($_.SubscriptionId, $_.Owner)} |
117 | | - |
118 | | -#Output Files |
119 | | -if (Test-Path -Path $CsvFile -ErrorAction SilentlyContinue) { |
120 | | - if ($Force) { |
121 | | - Remove-Item -Path $CsvFile -Force |
122 | | - } |
123 | | - else { |
124 | | - Write-Host "$CsvFile alreday exists use -Force to overwrite" |
125 | | - return |
126 | | - } |
| 119 | +foreach ($subentry in $subs) { |
| 120 | + [void]($subtable.Add([pscustomobject] @{ |
| 121 | + SubscriptionId = $subentry.SubscriptionId |
| 122 | + Owner = $subentry.Owner |
| 123 | + DisplayName = $subentry.DisplayName |
| 124 | + } |
| 125 | + ) |
| 126 | + ) |
127 | 127 | } |
128 | 128 |
|
129 | | -$result = Get-AzsSubscriberUsage -ReportedStartTime ("{0:yyyy-MM-ddT00:00:00.00Z}" -f $StartTime) -ReportedEndTime ("{0:yyyy-MM-ddT00:00:00.00Z}" -f $EndTime) -AggregationGranularity $Granularity |
| 129 | +$usageSummary = New-Object System.Collections.ArrayList |
| 130 | + |
| 131 | +foreach ($subID in $subtable) { |
| 132 | + Write-Host "Retrieving SubscriptionID $($subID.SubscriptionId)" -ForegroundColor Yellow |
| 133 | + $result = Get-AzsSubscriberUsage -SubscriberId $subID.SubscriptionId -ReportedStartTime ("{0:yyyy-MM-ddT00:00:00.00Z}" -f $StartTime) -ReportedEndTime ("{0:yyyy-MM-ddT00:00:00.00Z}" -f $EndTime) -AggregationGranularity $Granularity |
130 | 134 |
|
131 | | -$usageSummary = @() |
132 | | -$result | ForEach-Object { |
133 | | - $record = New-Object -TypeName System.Object |
134 | | - $resourceInfo = ($_.InstanceData | ConvertFrom-Json).'Microsoft.Resources' |
135 | | - $resourceText = $resourceInfo.resourceUri |
136 | | - $subscription = $resourceText.Split('/')[2] |
137 | | - $resourceType = $resourceText.Split('/')[7] |
138 | | - $resourceGroup = $resourceText.Split('/')[4] |
139 | | - $resourceName = $resourceText.Split('/')[8] |
140 | | - $record | Add-Member -Name UsageStartTime -MemberType NoteProperty -Value $_.UsageStartTime |
141 | | - $record | Add-Member -Name UsageEndTime -MemberType NoteProperty -Value $_.UsageEndTime |
142 | | - $record | Add-Member -Name MeterName -MemberType NoteProperty -Value $meters[$_.MeterId] |
143 | | - $record | Add-Member -Name Quantity -MemberType NoteProperty -Value $_.Quantity |
144 | | - $record | Add-Member -Name resourceType -MemberType NoteProperty -Value $resourceType |
145 | | - $record | Add-Member -Name location -MemberType NoteProperty -Value $resourceInfo.location |
146 | | - $record | Add-Member -Name resourceGroup -MemberType NoteProperty -Value $resourceGroup |
147 | | - $record | Add-Member -Name resourceName -MemberType NoteProperty -Value $resourceName |
148 | | - $record | Add-Member -Name subowner -MemberType NoteProperty -Value $subtable[$subscription] |
149 | | - $record | Add-Member -Name tags -MemberType NoteProperty -Value $resourceInfo.tags |
150 | | - $record | Add-Member -Name MeterId -MemberType NoteProperty -Value $_.MeterId |
151 | | - $record | Add-Member -Name additionalInfo -MemberType NoteProperty -Value $resourceInfo.additionalInfo |
152 | | - $record | Add-Member -Name subscription -MemberType NoteProperty -Value $subscription |
153 | | - $record | Add-Member -Name resourceUri -MemberType NoteProperty -Value $resourceText |
154 | | - $usageSummary += $record |
| 135 | + foreach ($entry in $result) { |
| 136 | + $resourceInfo = ($entry.InstanceData | ConvertFrom-Json).'Microsoft.Resources' |
| 137 | + $resourceText = $resourceInfo.resourceUri |
| 138 | + $subscription = $resourceText.Split('/')[2] |
| 139 | + $resourceType = $resourceText.Split('/')[7] |
| 140 | + $resourceGroup = $resourceText.Split('/')[4] |
| 141 | + $resourceName = $resourceText.Split('/')[8] |
| 142 | + |
| 143 | + [void]($usageSummary.Add([pscustomobject] @{ |
| 144 | + UsageStartTime = $entry.UsageStartTime |
| 145 | + UsageEndTime = $entry.UsageEndTime |
| 146 | + MeterName = $meters[$entry.MeterId] |
| 147 | + Quantity = $entry.Quantity |
| 148 | + resourceType = $resourceType |
| 149 | + location = $resourceInfo.location |
| 150 | + resourceGroup = $resourceGroup |
| 151 | + resourceName = $resourceName |
| 152 | + subowner = $subID.Owner |
| 153 | + tags = $resourceInfo.tags |
| 154 | + MeterId = $entry.MeterId |
| 155 | + additionalInfo = $resourceInfo.additionalInfo |
| 156 | + subscription = $subscription |
| 157 | + resourceUri = $resourceText |
| 158 | + } |
| 159 | + ) |
| 160 | + ) |
| 161 | + } |
155 | 162 | } |
156 | 163 | $usageSummary | Export-Csv -Path $CsvFile -NoTypeInformation |
| 164 | + |
0 commit comments