Public/Out-ALHHtmlDoc.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
<#PSScriptInfo
 
.VERSION 1.3.4
 
.GUID 1c1ad99a-dbb6-41cb-b269-81b7d2891928
 
.AUTHOR Dieter Koch
 
.COMPANYNAME
 
.COPYRIGHT (c) Dieter Koch. All rights reserved.
 
.TAGS
 
.LICENSEURI https://github.com/admins-little-helper/ALH/blob/main/LICENSE
 
.PROJECTURI https://github.com/admins-little-helper/ALH
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
    1.0.0
    Initial Release
 
    1.1.0
    Added possibility to collapse a section
 
    1.2.0
    Remove parameter 'AddToc' and added paramter 'AddTableRowToButton'
 
    1.3.0
    Updated Css.
    Replaced JavaScript code for sorting table function
 
    1.3.1
    Fixed Css to make tbody vertically scrollable
 
    1.3.2
    Updated CSS for 'reportFooter'
    Removed replacing linebreaks with '<br/>' html tag because this is done in Out-ALHHtmlReport already
 
    1.3.3
    Updated CSS for showing whole table in a block and remove thead and tbody styles. This way, the header and body columns have the same width
        and the table gets horizontal and vertical scroll bars if it can not be displayed completely.
 
    1.3.4
    Updated CSS for 'table' element type (set font-size to 12px)
 
#>


<#
 
.DESCRIPTION
Contains a function to create a html document out of ALHHtmlTables.
 
Some helpful information was found here:
    https://www.kryogenix.org/code/browser/sorttable/
    https://stackoverflow.com/questions/59282842/how-to-make-sorting-html-tables-faster
#>



function Out-ALHHtmlDoc {
    <#
    .SYNOPSIS
    A PowerShell function to create a html document out of ALHHtmlTables.
 
    .DESCRIPTION
    This functions takes one or multiple 'ALHHtmlTable' objects as input and creates a html document.
    The given html tables will be shown one after another in the resulting html document with their own
    titles, subtitles, footers etc.
 
    .PARAMETER HtmlReport
    One or multiple PFCHtmlReport objects to create the document from.
 
    .PARAMETER Title
    A title for the document.
 
    .PARAMETER Subtitle
    A subtitle for the document.
 
    .PARAMETER Infotext
    This text will be shown in the header.
 
    .PARAMETER Footer
    This text will be shown at the document footer.
 
    .PARAMETER MainBackgroundColor
    Color name for the document background color.
 
    .PARAMETER MainBackgroundColorHexcode
    Color hex code for the document background color.
 
    .PARAMETER Font
    Font used for the whole html document. Default value is 'Verdana'.
 
    .PARAMETER FontInfoText
    Font used for the html report info text. Default values is 'Courier New'.
 
    .PARAMETER AddTableRowCountToButton
    If specified, the value of the 'TableRowCount' property of the ALHHtmlReport class object is added to the button text.
 
    .EXAMPLE
    Out-ALHHtmlDoc -HtmlReport $HtmlReport -Title "DocTitle" -SubTitle "DocSubtitle" -InfoText "DocInfoText" -Footer "DocFooter" -MainBackgroundColorHexcode "#3366cc" | Out-File -FilePath C:\temp\testhtml4.html
 
    $HtmlReport = Get-Process | Select-Object -Propert Name,ID | Out-ALHHtmlTable -Title "Process on my computer" -Subtitle "Process list" -Infotext "A list of processes running a my computer" -Footer "Process list at $(Get-Date)" -AddSort -AddFilter
 
    .INPUTS
    ALHHtmlReport
 
    .OUTPUTS
    String
 
    .NOTES
    Author: Dieter Koch
    Email: diko@admins-little-helper.de
 
    .LINK
    https://github.com/admins-little-helper/ALH/blob/main/Help/Out-ALHHtmlDoc.txt
    #>


    [CmdletBinding(DefaultParameterSetName = "default")]
    param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [ALHHtmlReport[]]
        $HtmlReport,

        [string]
        $Title = "HTML Report",

        [string]
        $SubTitle,

        [string]
        $InfoText,

        [string]
        $Footer,

        [Parameter(ParameterSetName = "ColorName")]
        [ValidateSet("Red", "DarkRed", "LightRed", "Yellow", "DarkYellow", "LightYellow", "Blue", "DarkBlue", "LightBlue", "Green", "DarkGreen", "LightGreen", "Purple", "DarkPurple", "LightPurple", "Orange", "DarkOrange", "LighOrange")]
        [string]
        $MainBackgroundColor = "blue",

        [Parameter(ParameterSetName = "ColorCode")]
        [string]
        $MainBackgroundColorHexcode = "#0066a1",

        [string]
        $Font = "Verdana",

        [string]
        $FontInfoText = "Courier New",

        [switch]
        $AddTableRowCountToButton
    )

    begin {
        $Colors = [ordered]@{
            'Red'         = '#ff0000'
            'DarkRed'     = '#990000'
            'LightRed'    = '#ffcccc'
            'Yellow'      = '#ffff00'
            'DarkYellow'  = '#ffff00'
            'LightYellow' = '#ffff00'
            'Blue'        = '#0000ff'
            'DarkBlue'    = '#000099'
            'LightBlue'   = '#ccccff'
            'Green'       = '#00ff55'
            'DarkGreen'   = '#009933'
            'LightGreen'  = '#ccffdd'
            'Purple'      = '#cc00cc'
            'DarkPurple'  = '#990099'
            'LightPurple' = '#ffccff'
            'Orange'      = '#ff6600'
            'DarkOrange'  = '#993d00'
            'LighOrange'  = '#ffe0cc'
        }

        if ($PSBoundParameters.ContainsKey('MainBackgroundColor')) {
            $MainBackgroundColorValue = $Colors[$MainBackgroundColor]
        }
        elseif ($PSBoundParameters.ContainsKey('MainBackgroundColorHexcode')) {
            $MainBackgroundColorValue = $MainBackgroundColorHexcode
        }

        [string]$CurrentDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        [string]$CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name.ToLower()

        if ([string]::IsNullOrEmpty($MyInvocation.PSCommandPath)) {
            $ScriptPath = "$((Get-Location).Path) --> $($MyInvocation.MyCommand)"
        }
        else {
            $ScriptPath = $MyInvocation.PSCommandPath
        }

        [string]$HtmlDocFooter = @"
            <table id=`"Table_HtmlDocFooter`">
                <tr>
                    <td class="docFooter">$Footer</td>
                </tr>
            </table>
            <div class='docSectionRow'>Report created: $CurrentDate &#149; Computername: $env:COMPUTERNAME &#149; Username: $CurrentUser &#149; ScriptPath: $ScriptPath</div>
"@


        [string]$Css = @"
            <style type=`"text/css`">
            html body {
                font-family: $Font;
                font-size: 12px;
                height: 100%;
                margin: 0;
                overflow: auto;
            }
 
            #docHeader {
                background: $MainBackgroundColorValue;
                color: #ffffff;
                width: 100%
            }
 
            #docHeaderTop {
                padding: 10px;
            }
 
            .docTitle {
                float: left;
                font-size: 24px;
                font-weight: bold;
                padding: 0 7px 0 0;
            }
 
            .docSubtitle {
                float: left;
                font-size: 24px;
            }
 
            .docInfoText {
                float: right;
                font-size: 12px;
                text-align: right;
            }
 
            #reportHeader {
                background: $MainBackgroundColorValue;
                color: #ffffff;
                width: 100%;
                margin: 5px 5px 0px 5px;
            }
 
            #reportHeaderTop {
                padding: 10px 10px 10px 20px;
            }
 
            .reportTitle {
                float: left;
                font-size: 20px;
                font-weight: bold;
                padding: 0 7px 0 0;
                width: 100%;
            }
 
            .reportSubtitle {
                float: left;
                font-size: 16px;
                padding: 5px 0 0 20px;
                width: 100%;
            }
 
            .reportInfoText {
                padding: 10px 10px 10px 30px;
                float: left;
                font-family: $FontInfoText;
                font-size: 12px;
                text-align: left;
                color: #ffff00;
            }
 
            .headerRow {
                background: #66a3c7;
                height: 5px;
            }
 
            .reportTitleRow {
                background: #000000;
                color: #ffffff;
                font-size: 18px;
                padding: 10px;
                text-align: left;
            }
 
            .docSectionRow {
                background: #000000;
                color: #ffffff;
                font-size: 12px;
                padding: 3px 5px !important;
                font-weight: bold;
            }
 
            .reportSeparationRow {
                background: #ffffff;
                color: #ffffff;
                font-size: 12px;
                padding: 1px 5px !important;
                font-weight: bold;
                height: 15px !important;
            }
 
            .docFooter {
                background: $MainBackgroundColorValue;
                color: #ffffff;
                font-size: 14px;
                padding: 1px 5px !important;
                font-weight: bold;
                height: 15px !important;
            }
 
            .reportFooter {
                background: #808080;
                color: #ffffff;
                font-size: 12px;
                padding: 1px 5px !important;
                font-weight: bold;
                min-height: 15px !important;
                margin: 5px;
            }
 
            table {
                background: #eaebec;
                border: #cccccc 1px solid;
                border-collapse: collapse;
                margin: 5px;
                width: 100%;
                table-layout: fixed;
                display: block;
                overflow: auto;
                max-height: 500px;
                text-align: left;
                font-size: 12px;
            }
 
            table th {
                background: #ededed;
                border-top: 1px solid #fafafa;
                border-bottom: 1px solid #e0e0e0;
                border-left: 1px solid #e0e0e0;
                height: 45px;
                min-width: 55px;
                padding: 0px 15px;
            }
 
            table td {
                border-top: 1px solid #ffffff;
                border-bottom: 1px solid #e0e0e0;
                border-left: 1px solid #e0e0e0;
                padding: 0px 10px;
            }
 
            table tr:last-child td {
                border-bottom: 0;
            }
 
            table tr:hover td {
                background: #f2f2f2;
            }
 
            table tr:hover td.sectionRow {
                background: #0066a1;
            }
 
            table tr:nth-child(odd) {
                background: #b8d1f3;
            }
 
            table tr:nth-child(even) {
                background: #dae5f4;
            }
 
            table tr:nth-child(odd):hover {
                background: #588dbb;
            }
 
            table tr:nth-child(even):hover {
                background: #8994a2;
            }
 
            /* Style table header showing up/down arrows based on applied sort order */
            table.sortable th:not(.sorttable_sorted):not(.sorttable_sorted_reverse):not(.sorttable_nosort):after {
                content: " \25B4\25BE"
            }
 
            .line {
                width: 100%;
                height: 47px;
                border-bottom: 1px solid #ffff00;
                margin-left: 20px;
                margin-right: 20px;
            }
 
            /* Style the button that is used to open and close the collapsible content */
            .collapsible {
                background-color: #eee;
                color: #444;
                cursor: pointer;
                padding: 18px;
                width: 100%;
                border: none;
                text-align: left;
                outline: none;
                font-size: 15px;
            }
 
            .collapsible:after {
                content: '\02795';
                /* Unicode character for "plus" sign (+) */
                font-size: 13px;
                color: white;
                float: right;
                margin-left: 5px;
            }
 
            .active:after {
                content: "\2796";
                /* Unicode character for "minus" sign (-) */
            }
 
            .collapsible[value="0"] {
                /* If value of button equals "=", the button background will be made light green */
                background-color: #99e699
            }
 
            /* Add a background color to the button if it is clicked on (add the .active class with JS), and when you move the mouse over it (hover) */
            .active,
            .collapsible:hover {
                background-color: #ccc;
            }
 
            /* Style the collapsible content. Note: hidden by default */
            .content {
                padding: 0 18px;
                background-color: white;
                max-height: 0;
                overflow: hidden;
                transition: max-height 0.2s ease-out;
            }
        </style>
"@


        [string]$SortScript = @'
        <script type="text/javascript">
            /*
                taken from https://stackoverflow.com/questions/59282842/how-to-make-sorting-html-tables-faster
                and slithly adjusted to also sort text instead of just numbers
            */
 
            function sortTableRowsByColumn(table, columnIndex, ascending) {
                const rows = Array.from(table.querySelectorAll(':scope > tbody > tr'));
 
                rows.sort((x, y) => {
                    const xValue = x.cells[columnIndex].textContent;
                    const yValue = y.cells[columnIndex].textContent;
 
                    const xNum = parseFloat(xValue);
                    const yNum = parseFloat(yValue);
 
                    if (isNaN(xNum)) {
                        return ascending ? ('' + x.cells[columnIndex].textContent).localeCompare('' + y.cells[columnIndex].textContent) : ('' + y.cells[columnIndex].textContent).localeCompare('' + x.cells[columnIndex].textContent);
                    }
                    else {
                        return ascending ? (xNum - yNum) : (yNum - xNum);
                    }
 
                });
 
                rows.sort();
 
                for (let row of rows) {
                    table.tBodies[0].appendChild(row);
                }
            }
 
            function onColumnHeaderClicked(ev) {
                const th = ev.currentTarget;
                const table = th.closest('table');
                const thIndex = Array.from(th.parentElement.children).indexOf(th);
 
                const ascending = !('sort' in th.dataset) || th.dataset.sort != 'asc';
 
                const start = performance.now();
 
                sortTableRowsByColumn(table, thIndex, ascending);
 
                const end = performance.now();
                console.log("Sorted table rows in %d ms.", end - start);
 
                const allTh = table.querySelectorAll(':scope > thead > tr > th');
                for (let th2 of allTh) {
                    delete th2.dataset['sort'];
                }
 
                th.dataset['sort'] = ascending ? 'asc' : 'desc';
            }
        </script>
'@


        [string]$FilterScript = @"
            <script type="text/javascript">
                function filterTable(TableId, ColumnIndex) {
                    var input, filter, table, tr, td, i, txtValue, filterColumn;
                    filterColumn = "myInput_" + TableId + "_" + ColumnIndex
                    input = document.getElementById(filterColumn);
                    filter = input.value.toUpperCase();
                    table = document.getElementById(TableId);
                    tr = table.getElementsByTagName("tr");
                    for (i = 0; i < tr.length; i++) {
                        td = tr[i].getElementsByTagName("td")[ColumnIndex];
                        if (td) {
                            txtValue = td.textContent || td.innerText;
                            if (txtValue.toUpperCase().indexOf(filter) > -1) {
                                tr[i].style.display = "";
                            } else {
                                tr[i].style.display = "none";
                            }
                        }
                    }
                }
 
                function makeTablesSortable() {
                    var coll = document.getElementsByTagName("th");
                    var i;
 
                    for (i = 0; i < coll.length; i++) {
                        coll[i].addEventListener("click", function () {
                            onColumnHeaderClicked(event);
                        });
                    }
                }
            </script>
"@


        [string]$CollapsScript = @'
            <script type="text/javascript">
                function collapseSection() {
                    var coll = document.getElementsByClassName("collapsible");
                    var i;
 
                    for (i = 0; i < coll.length; i++) {
                        coll[i].addEventListener("click", function () {
                            this.classList.toggle("active");
                            var content = this.nextElementSibling;
 
                            if (content.style.maxHeight) {
                                content.style.maxHeight = null;
                            } else {
                                content.style.maxHeight = content.scrollHeight + "px";
                            }
                        });
                    }
                }
            </script>
'@


        [string]$DomLoadedScript = @'
            <script type="text/javascript">
                document.addEventListener("DOMContentLoaded", function () {
                    // do things after the DOM loads partially
                    console.log("DOM is loaded");
                    collapseSection();
                    makeTablesSortable();
                });
            </script>
'@


        $CRLF = "`r`n"
    }

    process {
        [string]$body = ""

        $body = $body + "<div id=`"docHeader`">"
        $body = $body + "<div id=`"docHeaderTop`">"
        $body = $body + "<div class=`"docTitle`">$Title</div>"
        $body = $body + "<div class=`"docSubtitle`">$SubTitle</div>"
        $body = $body + "<div class=`"docInfoText`">$InfoText</div>"
        $body = $body + "<div style=`"clear:both;`"></div></div>"
        $body = $body + "<div style=`"clear:both;`"></div>"
        $body = $body + "</div>"

        foreach ($SingleReport in $htmlReport) {
            if ($AddTableRowCountToButton.IsPresent) {
                $body = $body + "<button type=`"button`" class=`"collapsible`" value=`"$($SingleReport.TableRowCount)`">[$($SingleReport.TableRowCount)] $($SingleReport.Title)</button>$CRLF"
            }
            else {
                $body = $body + "<button type=`"button`" class=`"collapsible`" value=`"$($SingleReport.TableRowCount)`">$($SingleReport.Title)</button>$CRLF"
            }

            $body = $body + "<div class=`"content`">$CRLF"
            $body = $body + "<div id=`"reportHeader`">$CRLF"
            $body = $body + "<div id=`"reportHeaderTop`">$CRLF"
            $body = $body + "<div class=`"reportTitle`">$($SingleReport.Title)</div>$CRLF"
            $body = $body + "</div>$CRLF"
            $body = $body + "<div class=`"reportSubtitle`">$($SingleReport.SubTitle)</div>$CRLF"
            $body = $body + "<div class=`"line`"></div>"
            $body = $body + "<div class=`"reportInfoText`">$($SingleReport.InfoText)</div>$CRLF"
            $body = $body + "<div style=`"clear:both;`"></div></div>$CRLF"
            $body = $body + "<div style=`"clear:both;`"></div>$CRLF"

            if ($SingleReport.Filter) {
                $body = $body + "$($SingleReport.HtmlTableFilter) $CRLF"
            }

            $body = $body + "$($SingleReport.HtmlTable)$CRLF $CRLF"
            $body = $body + "<div class=`"reportFooter`">$($SingleReport.Footer)</div>$CRLF"
            $body = $body + "<div class=`"reportSeparationRow`"> &nbsp; </div>$CRLF"
            $body = $body + "<div style=`"clear:both;`"></div></div>$CRLF"
        }

        $body = ("$body $CRLF $CRLF $HtmlDocFooter")
    }

    end {
        $Head = "$Css $CRLF $CRLF <title>$Title</title>"
        $Head = ("$DomLoadedScript $CRLF $CRLF $Head")
        $Head = ("$CollapsScript $CRLF $CRLF $Head")

        if ($htmlReport.Sort) {
            $Head = ("$SortScript $CRLF $CRLF $Head")
        }

        if ($htmlReport.Filter) {
            $Head = ("$FilterScript $CRLF $CRLF $Head")
        }

        $htmlDoc = ConvertTo-Html -Body $body -Head $Head
        # remove an emtpy table tag added by the ConvertTo-Html
        $htmlDoc = ($htmlDoc | Out-String) -replace "(<table>\s+<\/table>)", ""

        return $htmlDoc
    }
}

#region EndOfScript
<#
################################################################################
################################################################################
#
# ______ _ __ _____ _ _
# | ____| | | / _| / ____| (_) | |
# | |__ _ __ __| | ___ | |_ | (___ ___ _ __ _ _ __ | |_
# | __| | '_ \ / _` | / _ \| _| \___ \ / __| '__| | '_ \| __|
# | |____| | | | (_| | | (_) | | ____) | (__| | | | |_) | |_
# |______|_| |_|\__,_| \___/|_| |_____/ \___|_| |_| .__/ \__|
# | |
# |_|
################################################################################
################################################################################
# created with help of http://patorjk.com/software/taag/
#>

#endregion