-
Notifications
You must be signed in to change notification settings - Fork 15
/
Inherit-AzTag.ps1
153 lines (141 loc) · 5.78 KB
/
Inherit-AzTag.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
141
142
143
144
145
146
147
148
149
150
151
152
153
<#
.SYNOPSIS
Inherit the resource group's tag to all it's sub resources.
.DESCRIPTION
REQUIRED : Connected to an Azure subscription with the contributor role on the resource group
.PARAMETER action
Mandatory
Supported values : inherit_from_rg
.PARAMETER resource_group
Mandatory
Resource group name
.NOTES
AUTHOR: James Dumont le Douarec
.LINK
https://github.com/JamesDLD/AzureRm-PowerShell
.EXAMPLE
1. Inherit all tags of the resource group "apps-jdld-sand1-rg1" to all it's sub resources
.\Inherit-AzTag.ps1 -resource_group "rg-usabilla-dev"
1. Inherit all tags from all resource groups to all their sub resources except for some Resource Group
$rgs_toskip = @("databricks-managed-rg1")
$rgs = get-azresourcegroup | Where-Object { $_.ResourceGroupName -notin $rgs_toskip }
foreach ($rg in $rgs.ResourceGroupName)
{
write-host "$rg"
.\Inherit-AzTag.ps1 -resource_group $rg
write-host ""
}
#>
param(
[Parameter(Mandatory=$false,HelpMessage='Action')]
[String]
$action="inherit_from_rg",
[Parameter(Mandatory=$true,HelpMessage='Resource Group')]
[String]
$resource_group
)
################################################################################
# Function
################################################################################
#region function
Function Generate_Log_Action([string]$Action, [ScriptBlock]$Command, [string]$LogFile) {
$Output = "Info : $Action ... "
Write-Host $Output -ForegroundColor Cyan
((Get-Date -UFormat "[%d-%m-%Y %H:%M:%S] : ") + "Info" + " : " + $Action) | Out-File -FilePath $LogFile -Append -Force
Try {
$Result = Invoke-Command -ScriptBlock $Command
}
Catch {
$ErrorMessage = $_.Exception.Message
$Output = "On action $Action : $ErrorMessage"
((Get-Date -UFormat "[%d-%m-%Y %H:%M:%S] : ") + "Error" + " : " + $Output) | Out-File -FilePath $LogFile -Append -Force
Write-Error $Output
$Result = "Error"
}
Return $Result
}
#endregion
################################################################################
# Variable
################################################################################
Set-StrictMode -Version 2
$ErrorActionPreference = "Stop"
$workfolder = Split-Path $script:MyInvocation.MyCommand.Path
$date = Get-Date -UFormat "%d-%m-%Y"
$count=0
$ResourceTypesToExclude = @("Microsoft.Insights/autoscaleSettings","Microsoft.Compute/virtualMachines/extensions","Microsoft.ClassicNetwork/virtualNetworks","Microsoft.ClassicStorage/storageAccounts")
#Local log file
$LogPath = $workfolder + "\logs"
if (!(Test-Path $LogPath)) { mkdir $LogPath }
$logFile = $LogPath + "\$date-" + $MyInvocation.MyCommand.Name + ".log"
#Action
switch($action){
inherit_from_rg {
Try{
#List all Resources within the Resource Group
$RGTags = (Get-AzResourceGroup -Name $resource_group).Tags
$Resources = Get-AzResource -ResourceGroupName $resource_group -ErrorAction Stop | Where-Object { $_.ResourceType -notin $ResourceTypesToExclude }
#For each Resource apply the Tag of the Resource Group
Foreach ($resource in $Resources)
{
$resourceid = $resource.resourceId
$resourcetags = $resource.Tags
If ($resourcetags -eq $null)
{
Write-Output "---------------------------------------------"
$Action = "NEW - Applying the following Tags to $($resourceid) : $([string[]]$($RGTags | out-string -stream))"
$Command = {Set-AzResource -ResourceId $resourceid -Tag $RGTagS -Force -ErrorAction Stop}
$Result = Generate_Log_Action -Action $Action -Command $Command -LogFile $logFile
if($Result -eq "Error"){Exit 1}
Write-Output "---------------------------------------------"
$count++
}
Else
{
$TagUpdate=$false
Foreach ($RGTag in $RGTags.GetEnumerator())
{
#Checking if Tags keys of the resource group are all in the resource's tag keys
If ($resourcetags.Keys -inotcontains $RGTag.Key)
{
Write-Output "------------------------------------------------"
Write-Output "Key = $($RGTag.Key) doesn't exist"
$resourcetags.Add($RGTag.Key,$RGTag.Value)
$TagUpdate=$true
}
Else
{
if ($resourcetags.Item($RGTag.Key) -ne $RGTag.Value)
{
Write-Output "------------------------------------------------"
Write-Output "Key = $($RGTag.Key) doesn't have the RG Tag value = $($RGTag.Value), it's value is = $($resourcetags.Item($RGTag.Key))"
$resourcetags.Remove($RGTag.Key)
$resourcetags.Add($RGTag.Key,$RGTag.Value)
$TagUpdate=$true
}
}
}
if($TagUpdate)
{
Write-Output "---------------------------------------------"
$Action = "UPDTATE - Applying the following Tags to $($resourceid) : $([string[]]$($RGTags | out-string -stream))"
$Command = {Set-AzResource -ResourceId $resourceid -Tag $resourcetags -Force -ErrorAction Stop}
$Result = Generate_Log_Action -Action $Action -Command $Command -LogFile $logFile
if($Result -eq "Error"){Exit 1}
Write-Output "---------------------------------------------"
$count++
}
}
}
$body = "$count resources have been tagged"
$status = "OK"
}
Catch {$body = $_.Exception.Message;$status = "Unauthorized"}
}
default {
$status = "BadRequest"
$body="Invalid action. Allowed values : inherit_from_rg."
}
}
Write-Output "Status = $status"
Write-Output "$body"