-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReportMissingTags.ps1
35 lines (28 loc) · 1.38 KB
/
ReportMissingTags.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
<#
Script: report_azure_missingtags.ps1
Author: Adeel Anwar
Description:
Scan all subscription searching the value stored in $key
If its missing report it in an excel sheet. Place all resources affected
#>
[System.Collections.ArrayList]$FindOwnerTags = New-Object -TypeName System.Collections.ArrayList
$SubscriptionLists = Get-AzSubscription
[string]$key = '<InsertTagKey'
foreach ($subscriptionlist in $subscriptionlists) {
$context = Set-AzContext $subscriptionlist
$ResourceTags = Get-AzResource
foreach ($Resourcetag in $Resourcetags) {
if ($null -eq ($Resourcetag | Where-object {$_.Tags.keys -eq $Key})){ # Does Not Contain Primary Tag
$TagDetails = [ordered]@{ #Store data in the following, in this order as variables
SubscriptionName = $context.Subscription.Name
Resource = $Resourcetag.name
ResourceGroupName = $Resourcetag.ResourceGroupName
Tags = (($resourcetag | Select-Object tags | out-string -stream).TrimStart("{, Tags, ----, ") | out-string).trim() # Converts hashtable to a single string line
}
}
$FindOwnerTags.add((New-Object psobject -Property $TagDetails)) | Out-Null
}
}
# $FindOwnerTags | Format-table -AutoSize
$FileName = '.\MissingTags_' + ($key -replace '[:]','') + '.csv'
$FindOwnerTags | Export-Csv -Path $FileName -NoTypeInformation