-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTMLDiskReport.ps1
391 lines (318 loc) · 12.7 KB
/
HTMLDiskReport.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#requires -version 3.0
Function New-HTMLDiskReport {
<#
.Synopsis
Create a disk utilization report with gradient coloring
.Description
This command will create an HTML report depicting drive utilization through a gradient color bar.
.Parameter Computername
The name(s) of computers to query. They must be running PowerShell 3.0 or later and support CIM queries.
This parameter has an alias of CN.
.Parameter ReportTitle
The HTML title to be for your report. This parameter has an alias of Title.
.Parameter Path
The filename and path for your html report.
.Parameter PreContent
Add any HTML text to insert before the drive utilization table.
.Parameter PostContent
Add any HTML text to insert after the drive utilization table.
.Parameter LogoPath
Specify the path to a PNG or JPG file to use as a logo image. The image will be embedded into the html file.
.Example
PS C:\> New-HTMLDiskReport -passthru
Directory: C:\Users\Jeff\AppData\Local\Temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 10/10/2018 3:58 PM 2493 utilization.htm
Create a report for the local host using default settings.
.Example
PS C:\> get-content c:\work\computers.txt | New-HTMLDiskReport -path c:\work\diskreport.htm
Create a single report for every computer listed in computers.txt. The report will be saved to c:\work\diskreport.htm
.Example
PS C:\> New-HTMLDiskReport -Path c:\work\report.htm -Computername SRV1,SRV2,SRV3 -Precontent "<h3>Company Confidential</h3>" -PostContent "This report is offered as-is. You can verify results with a command like <b>Get-Volume</b>." -logopath c:\scripts\logo.png
Create a report for the specified servers and insert pre- and post-content.
.Notes
Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/
.Link
Get-CimInstance
.Link
Get-Volume
.Link
Get-Disk
.Inputs
System.String
#>
[cmdletbinding(SupportsShouldProcess)]
[OutputType("None", "System.IO.FileInfo")]
Param(
[Parameter(Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[ValidateNotNullorEmpty()]
[Alias("cn")]
[string[]]$Computername = $env:Computername,
[ValidateNotNullorEmpty()]
[Parameter(HelpMessage = "The report title")]
[Alias("title")]
[string]$ReportTitle = "Drive Utilization Report",
[Parameter(HelpMessage = "The filename and path for the finished HTML report.")]
[ValidateNotNullorEmpty()]
[ValidateScript( {
#get parent
$parent = Split-Path $_
if (Test-Path $parent) {
$True
}
else {
Throw "Can't verify part of the file path $_"
}
})]
[string]$Path = "$env:temp\utilization.htm",
[string[]]$PreContent,
[string[]]$PostContent,
[string]$LogoPath,
[switch]$Passthru
)
Begin {
Write-Verbose "Starting $($MyInvocation.Mycommand)"
#define HTML header with style elements. If using a header the title must be inserted here.
#here strings must be left justified
$head = @"
<style>
body {
background-color: #FFFFFF;
font-family: Tahoma;
font-size: 12pt;
}
td,
th {
border: 0px solid black;
border-collapse: collapse;
}
th {
color: white;
background-color: black;
}
table,
tr,
td,
th {
padding: 2px;
margin: 0px;
}
tr:nth-child(even) {
background-color: lightgray
}
table {
width: 95%;
margin-left: 10px;
margin-bottom: 20px;
table-layout: fixed;
}
.meta {
width: 25%;
font-size: 8pt;
margin-left: 0px;
table-layout: auto;
}
.top {
width: 50%;
margin-left: 0px;
table-layout: auto;
}
tr.meta {
background-color: #FFFFFF;
}
.right {
text-align: right;
width: 20%;
}
caption {
background-color: #FFFF66;
text-align: left;
font-weight: bold;
font-size: 14pt;
}
td[tip]:hover {
color: #ff2283;
position: relative;
}
td[tip]:hover:after {
content: attr(tip);
left: 0;
top: 100%;
margin-left: 80px;
margin-top: 10px;
width: 400px;
padding: 3px 8px;
position: absolute;
color: #85003a;
font-family: 'Courier New', Courier, monospace;
font-size: 10pt;
background-color: gainsboro;
white-space: pre-wrap;
}
</style>
<Title>$reportTitle</Title>
"@
<#
Define a here string for coloring percentage cells.
The starting and ending percents will need to provided
using the -f operator.
#>
$gradient = @"
filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0,
StartColorStr=#0A802D, EndColorStr=#FF0011)
background-color: #376C46;
background-image: -mso-linear-gradient(left, #0A802D {0}%, #FF0011 {1}%);
background-image: -ms-linear-gradient(left, #0A802D {0}%, #FF0011 {1}%);
background-image: -moz-linear-gradient(left, #0A802D {0}%, #FF0011 {1}%);
background-image: -o-linear-gradient(left, #0A802D {0}%, #FF0011 {1}%);
background-image: -webkit-linear-gradient(left, #0A802D {0}%, #FF0011 {1}%);
background-image: linear-gradient(left, #0A802D {0}%, #FF0011 {1}%);
color:white;
"@
If ($LogoPath) {
if (Test-Path $LogoPath) {
#insert a graphic
$ImageBits = [Convert]::ToBase64String((Get-Content $LogoPath -Encoding Byte))
$ImageFile = Get-Item $LogoPath
$ImageType = $ImageFile.Extension.Substring(1) #strip off the leading .
$ImageTag = "<Img src='data:image/$ImageType;base64,$($ImageBits)' Alt='$($ImageFile.Name)' style='float:left' width='120' height='120' hspace=10>"
}
else {
Write-Warning "Could not find image file $LogoPath"
}
}
if ($ImageTag) {
$top = "<table class='top'><tr><td>$ImageTag</td><td><H1>$ReportTitle</H1></td></table><br>"
}
else {
$top = "<H1>$ReportTitle</H1><br>"
}
#define an array to hold HTML fragments
$fragments = @($top)
$fragments += $Precontent
$fragments += "<br><br>"
#define a parameter hashtable for Write-Progress
$progParam = @{
Activity = $MyInvocation.MyCommand
Status = "Gathering disk data"
CurrentOperation = ""
}
} #begin
Process {
#get the data for the report
foreach ($computer in $computername) {
Write-Verbose "Getting disk information for $Computer"
$progParam.CurrentOperation = $computer.toUpper()
Write-Progress @progParam
Try {
#create a temporary CIMSession
$cs = New-CimSession -ComputerName $computer -ErrorAction stop
#hashtable of parameters to splat to Get-Ciminstance
$paramHash = @{
Classname = "win32_logicaldisk"
filter = "drivetype=3"
CimSession = $cs
ErrorAction = "Stop"
}
if ($pscmdlet.ShouldProcess($Computer, "Get Disk Information")) {
$data = Get-CimInstance @paramHash
Write-Verbose "Formatting data"
#initialize a hashtable of for phsyical media
$hash = @{}
#Create a custom object for each drive
$drives = foreach ($item in $data) {
$Physical = $item | Get-CimAssociatedInstance -ResultClassName Win32_DiskPartition | Get-CimAssociatedInstance -ResultClassName Win32_DiskDrive
$hash.Add($item.DeviceID,$physical)
$prophash = [ordered]@{
Drive = $item.DeviceID
Volume = $item.VolumeName
SizeGB = $item.size / 1GB -as [int]
FreeGB = "{0:N4}" -f ($item.Freespace / 1GB)
PercentFree = [math]::Round(($item.Freespace / $item.size) * 100, 2)
}
New-Object PSObject -Property $prophash
} #foreach item
#convert drive objects to HTML but as an XML document
Write-Verbose "Converting to XML"
[xml]$html = $drives | ConvertTo-Html -Fragment
#add the computer name as the table caption
$caption = $html.CreateElement("caption")
$html.table.AppendChild($caption) | Out-Null
$html.table.caption = $data[0].SystemName
$pop = $html.CreateAttribute("title")
$pop.value = (Get-Ciminstance -ClassName Win32_OperatingSystem -Property caption -cimsession $cs).caption
$html.table.item("caption").attributes.append($pop) | Out-Null
#add physical media as a popup for each device
for ($i=1; $i -le $html.table.tr.count -1;$i++) {
$id = $html.table.tr[$i].ChildNodes[0]."#text"
$pop = $html.CreateAttribute("tip")
$props = ($hash.Item($id) | Select-Object -property Caption,SerialNumber,FirmwareRevision,Size,InterfaceType,SCSI* | Out-String).trim()
$pop.Value = $props
$html.table.tr[$i].ChildNodes[0].Attributes.append($pop) | Out-Null
}
#go through rows again and add gradient
Write-Verbose "Inserting gradient"
for ($i = 1; $i -le $html.table.tr.count - 1; $i++) {
$class = $html.CreateAttribute("style")
[int]$start = $html.table.tr[$i].td[-1]
#create the gradient using starting and ending values
#based on %free
$class.value = $Gradient -f $start, (100 - $start)
$html.table.tr[$i].ChildNodes[4].Attributes.Append($class) | Out-Null
} #for
#add the html to the fragments
$fragments += $html.InnerXml
} #should process
} #Try
Catch {
Write-Warning "Failed to get disk information for $computer. $($_.exception.message)"
} #Catch
remove-cimsession $cs
} #foreach computer
} #process
End {
$progParam.currentOperation = "Finalizing report"
Write-Progress @progParam
#only proceed if there is data in $fragments
if ($fragments) {
#add some metadata about this report
[xml]$metadata = [pscustomobject]@{
"Report Run" = "$((Get-Date).ToUniversalTime()) UTC"
"Run By" = "$($env:USERDOMAIN)\$env:username"
Originated = $env:Computername
Command = $($myinvocation.invocationname)
Version = "2.0"
} | ConvertTo-html -as List -Fragment
#insert css tags into this table
$class = $metadata.CreateAttribute("class")
$class.value = 'meta'
$metadata.table.Attributes.Append($class) | Out-Null
for ($i = 0; $i -le $metadata.table.tr.count - 1; $i++) {
$class = $metadata.CreateAttribute("class")
$class.value = 'meta'
$metadata.table.tr[$i].attributes.append($class) | Out-Null
$class = $metadata.CreateAttribute("class")
$class.value = 'right'
$metadata.table.tr[$i].item("td").attributes.append($class) | Out-Null
}
$postcontent += "<br><br>$($metadata.InnerXml)"
#create the final report
Write-Verbose "Creating HTML report"
$paramHash = @{
head = $head
Body = $($fragments | Out-String)
PostContent = $PostContent
}
if ($pscmdlet.ShouldProcess($path, "Creating HTML file")) {
ConvertTo-Html @paramHash | Out-File -FilePath $path -Encoding ascii
}
Write-Verbose "Report created to $path"
#if -Passthru write the file object to the pipeline
if ($Passthru) {
Get-Item $Path
}
}
Write-Verbose "Ending $($MyInvocation.Mycommand)"
} #end
} #close New-HTMLDiskReport