Run-ReportAzureResourceGroupTags.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

<#PSScriptInfo
 
.VERSION 1.0
 
.GUID 89267615-ea56-44a8-8465-6203b930e4df
 
.AUTHOR matt.quickenden
 
.COMPANYNAME ACE
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI http://www.azurefieldnotes.com/2018/02/08/reporting-on-resource-group-tags-in-azure
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
 
.DESCRIPTION
 A report to show Azure Resource Group Tags. This is a prototype.
  
 Sample Report
 https://azurefieldnotesblog.blob.core.windows.net/wp-content/2018/02/ResourceGroupTagsReportExample.jpg
 
 Please see this blog for details
 http://www.azurefieldnotes.com/2018/02/08/reporting-on-resource-group-tags-in-azure/
 
#>
 

#Requires �Modules ReportHTML

Param
(
    [parameter(Mandatory=$false,ValueFromPipeline = $true)] 
    [Array]$KeyNames,
    [parameter(Mandatory=$false)] 
    [String]$ReportOutputPath,
    [parameter(Mandatory=$false)] 
    [String]$YouLogoHereURLString 
)

[switch]$AutoKeyName =$false
$m = Get-Module -List ReportHTML
if(!$m) {"Can't locate module ReportHTML. Use Install-module ReportHTML";break}
else {import-module reporthtml}

if ([string]::IsNullOrEmpty($(Get-AzureRmContext).Account)) {Login-AzureRmAccount}

$RGs = Get-AzureRmResourceGroup 
if ($KeyNames.count -eq 0) 
{
    [switch]$AutoKeyName =$true
    $KeyNames = (($rgs.Tags.keys) | select -Unique)
}

$SubscriptionRGs = @()
foreach ($RG in $RGs) 
{

    $myRG = [PSCustomObject]@{
        ResourceGroupName     = $RG.ResourceGroupName
        Location = $RG.Location
        Link    =  ("URL01" + "https://portal.azure.com/#resource" + $RG.ResourceId  +  "URL02" + ($RG.ResourceId.Split('/') | select -last 1) + "URL03" )
    }

    $i=0
    foreach ($KeyName in $KeyNames) 
    {
        if ($AutoKeyName)
        {
            $myRG | Add-Member -MemberType NoteProperty -Name ([string]$i + "_" + $keyname) -Value $rg.Tags.($KeyName)
            $i++
        }
        else
        {
            $myRG | Add-Member -MemberType NoteProperty -Name ($keyname) -Value $rg.Tags.($KeyName)
        }
    }
    $SubscriptionRGs += $myRG
}


$rpt = @()
if ($YouLogoHereURLString -ne $null)
{
    $rpt += Get-HTMLOpenPage -TitleText "Azure Resource Groups" -LeftLogoString $YouLogoHereURLString -RightLogoString  "https://azurefieldnotesblog.blob.core.windows.net/wp-content/2017/02/ReportHTML.png"
}
else
{
    $rpt += Get-HTMLOpenPage -TitleText "Azure Resource Groups"
}

    if (!$AutoKeyName) 
    {
        $Pie1 = $SubscriptionRGs| group $KeyNames[0]
        $Pie2 = $SubscriptionRGs| group $KeyNames[1]

        $Pie1Object = Get-HTMLPieChartObject -ColorScheme Random
        $Pie2Object = Get-HTMLPieChartObject -ColorScheme Generated

        $rpt += Get-HTMLContentOpen -HeaderText "Pie Charts"
               $rpt += Get-HTMLColumnOpen -ColumnNumber 1 -ColumnCount 2
                   $rpt += Get-HTMLPieChart -ChartObject $Pie1Object  -DataSet $Pie1
               $rpt += Get-HTMLColumnClose
               $rpt += Get-HTMLColumnOpen -ColumnNumber 2 -ColumnCount 2
                   $rpt += Get-HTMLPieChart -ChartObject $Pie2Object -DataSet $Pie2
               $rpt += Get-HTMLColumnClose
        $rpt += Get-HTMLContentclose
    }

    $rpt += Get-HTMLContentOpen -HeaderText "Complete List"
        $rpt += Get-HTMLContentdatatable -ArrayOfObjects ( $SubscriptionRGs)
    $rpt += Get-HTMLContentClose 

$rpt += Get-HTMLClosePage  

if ($ReportOutputPath -ne $null)
{
    Save-HTMLReport -ShowReport -ReportContent $rpt -ReportName ResourceGroupTags
}
else
{
    Save-HTMLReport -ShowReport -ReportContent $rpt -ReportName ResourceGroupTags -ReportPath $ReportOutputPath 
}