-
Notifications
You must be signed in to change notification settings - Fork 1
/
Get-AllTagsCount.ps1
36 lines (32 loc) · 984 Bytes
/
Get-AllTagsCount.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
#Requires -module Logging, ImportExcel
$Subscriptions = Get-AzSubscription
$AllTags = @()
foreach($Subs in $Subscriptions){
$Subs | Set-AzContext
$AllTags += Get-AzTag
}
$CurrentDate = Get-Date
$ReportName = $($CurrentDate.ToString("yyyy-MM-dd")) + "_AllTags.xlsx"
$FinalTagsList = @()
# Merge duplicated tags keys in $AllTags
foreach($Tag in $AllTags){
$TagKey = $Tag.Name
if($TagKey -cin $FinalTagsList.Name){
continue
}
else{
$TagCounts = $AllTags | Where-Object {$_.Name -ceq $TagKey} | Select-Object -ExpandProperty Count
$TagSumm = 0
foreach($Count in $TagCounts){
$TagSumm += $Count
}
$FinalTagsList += New-Object PSObject -Property @{
Name = $TagKey
Count = $TagSumm
}
}
}
if(Test-Path $ReportName){
Remove-Item $ReportName
}
$FinalTagsList | Select-Object Name, Count | Export-Excel -Path $ReportName -AutoSize -AutoFilter -TableStyle Medium2