-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPSScriptAnalyzerReporter.ps1
More file actions
229 lines (201 loc) · 5.55 KB
/
PSScriptAnalyzerReporter.ps1
File metadata and controls
229 lines (201 loc) · 5.55 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
Param(
# Specifies a Path to the CSV output file of PSScriptAnalyser.
[Parameter(Mandatory=$true,
Position=0,
ParameterSetName="Path",
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
HelpMessage="Path to the CSV output file of PSScriptAnalyser.")]
[ValidateNotNullOrEmpty()]
[string]
$CsvPath
,
# Specifies the path to save the HTML report to.
[Parameter(Mandatory=$true,
Position=1,
ParameterSetName="Path",
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
HelpMessage="Path to save the HTML report to.")]
[ValidateNotNullOrEmpty()]
[string]
$OutputPath
,
# Specifies the friendly name of the report to show in the HTML
[Parameter(Mandatory=$false,
Position=2,
ParameterSetName="Path",
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
HelpMessage="The friendly name of the report to show in the HTML.")]
[ValidateNotNullOrEmpty()]
[string]
$ReportName = 'PSScriptAnalyzer Report'
)
if (!(Test-Path -Path $CsvPath))
{
Write-Error -Message "Cannot find CSV File: $CsvPath";
Exit;
}
# output file
$outputFile = Join-Path -Path $OutputPath -ChildPath "$(Split-Path -Path $CsvPath -Leaf).html";
# get the report
$scriptReportCsv = Import-Csv -Path $CsvPath;
$icons = @{
Information = 'images\Information_xsmall.png';
Success = 'images\Success_xsmall.png';
Warning = 'images\Warning_xsmall.png';
Critical = 'images\Critical_xsmall.png';
}
$cssPath = '.\css\PSScriptAnalyzerReporter.css';
[string]$htmlReportHdr = @"
<html>
<head>
<style>
.tblIssue
{
border-style: solid;
border-width: 1px;
width: 80%;
}
.tdBordered
{
border-style: solid;
border-width: 1px;
}
.tdCritical
{
background-color: DarkRed;
color: White;
}
.tdWarning
{
background-color: Orange;
color: Black;
}
.tdLow
{
background-color: Blue;
color: White;
}
.tdInformation
{
background-color: Green;
color: White;
}
.tdColHdr
{
align: center;
}
.tdText
{
align: Left;
}
.ReportHeader
{
font-size: 24pt;
align: center;
max-width: 800px;
}
.ReportSubHeader
{
font-size: 18pt;
align: center;
max-width: 800px;
}
.ReportDateTime
{
font-size: 12pt;
align: center;
max-width: 800px;
}
.Spacer
{
height: 15px;
}
</style>
</head>
<body>
<div class="ReportHeader">[ReportName]</div>
<div class="ReportDateTime">[ReportDateTime]</div>
"@;
[string]$htmlReportFtr = @"
<div class="spacer"></div>
<hr />
<div class="spacer"></div>
<div class="ReportDateTime">[ReportDateTime]</div>
</body>
</html>
"@;
[string]$htmlScriptHdr = @"
<hr />
<div class="spacer"></div>
<div class="ReportHeader">[ScriptName]</div>
"@
[string]$htmlScriptFtr = @"
<div class="spacer"></div>
<div class="spacer"></div>
"@
[string]$htmlIssueTemplate = @"
<table class="tblIssue">
<tr>
<td class="tdBordered tdColHdr"><img src="images\[Severity]_xsmall.png" alt="[Severity]"></td>
<th class="tdBordered tdText">[Message]</th>
</tr>
<tr>
<th class="tdBordered tdColHdr">Rule</th>
<td class="tdBordered tdText">[RuleName]</td>
</tr>
<tr>
<th class="tdBordered tdColHdr" rowspan="2">Source</th>
<td class="tdBordered tdText">[Extent]</td>
</tr>
<tr>
<td class="tdBordered tdText">Line: [Line] Column: [Column]</td>
</tr>
</table>
<div class="spacer"></div>
"@;
function Add-ReportValues
{
[CmdletBinding()]
Param(
[string]$Html
,
$object
)
$knownVariables = @('[ScriptName]'
, '[ReportDateTime]'
, '[Severity]'
, '[RuleName]'
, '[Message]'
, '[Line]'
, '[Column]'
, '[Extent]'
)
foreach($var in $knownVariables)
{
if ($Html.Contains($var))
{
$propName = $var.Replace('[','').Replace(']','');
if ($null -ne ($object | get-member -MemberType Properties -Name $propName))
{
$Html = $Html.Replace($var, $($object.$propName));
}
}
}
return $Html;
}
# build the report and replace the variables
Add-ReportValues -Html $htmlReportHdr -object ([PSCustomObject]@{ReportName = $ReportName; ReportDateTime = Get-Date;}) | Out-File -FilePath $outputFile;
# loop on scripts
foreach ($script in ($scriptReportCsv.ScriptName | Sort-object -Unique))
{
Add-ReportValues -Html $htmlScriptHdr -object ([PSCustomObject]@{ScriptName = $script;}) | Out-File -FilePath $outputFile -Append;
foreach ($issue in ($scriptReportCsv | Where-Object ScriptName -eq $script))
{
Add-ReportValues -Html $htmlIssueTemplate -object $issue | Out-File -FilePath $outputFile -Append;
}
Add-ReportValues -Html $htmlScriptFtr -object ([PSCustomObject]@{ScriptName = $script;}) | Out-File -FilePath $outputFile -Append;
}
Add-ReportValues -Html $htmlReportFtr -object ([PSCustomObject]@{ReportDateTime = Get-Date;}) | Out-File -FilePath $outputFile -Append;