-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertTo-HTMLTable.ps1
More file actions
119 lines (113 loc) · 3.91 KB
/
ConvertTo-HTMLTable.ps1
File metadata and controls
119 lines (113 loc) · 3.91 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
<#
<-ConvertTo-HTMLTable->
.SYNOPSIS
Converts array of powershell objects into a HTML table format
.DESCRIPTION
The headers for the table can be added to the top or left side of the table this can be distignuished by the <th> tag.
Note: No styling is added using this function only the data inbetween the <table></table> tags is created
.PARAMETER InputArray
The array of objects that needs to be converted
.PARAMETER ColumnsOrder
the order the given columns should be adhered to
.PARAMETER NoHeaders
a switch param to remove all headers form the table
.PARAMETER Horizontal
this Switch will move the headers to the left side of the table
.PARAMETER CenterData
This switch will center all information between <td></td> tags
#>
function ConvertTo-HTMLTable {
[CmdletBinding()]
Param(
[Parameter(
Mandatory = $true)][Array]$InputArray,
[Parameter(
Mandatory = $true)][Array]$ColumnsOrder,
[Parameter(
Mandatory = $false)][Array]$ErrorValues,
[Parameter(
Mandatory = $false)][Switch]$NoHeaders,
[Parameter(
Mandatory = $false)][Switch]$Horizontal,
[Parameter(
Mandatory = $false)][Switch]$CenterData
)
$Body = '<table>'
if ($CenterData) {
$inlineCenter = 'style="text-align:center;"'
}
if (!$NoHeaders) {
if (!$Horizontal) {
$body += '<tr>'
foreach ($Column in $ColumnsOrder) {
$Body += '<th>' + $Column + '</th>'
}
$body += '</tr>'
foreach ($Object in $InputArray) {
if ($ErrorValues) {
for ($i = 1; $i -le $ErrorValues.count; $i++) {
if ($Object -imatch $ErrorValues[$i - 1]) {
$rowStart = '<tr class="error">'
break
}
elseif ($i -eq $errorValues.count) {
$rowStart = '<tr>'
}
}
}
else {
$rowStart = '<tr>'
}
$body += $rowStart
foreach ($Column in $ColumnsOrder) {
$Body += "<td $inlineCenter>" + $Object.($Column) + '</td>'
}
$body += '</tr>'
}
}
elseif ($Horizontal) {
foreach ($Column in $ColumnsOrder) {
$body += '<tr><th>' + $Column + '</th>'
foreach ($Object in $InputArray) {
$body += "<td $inlineCenter>" + $Object.($Column) + '</td>'
}
$body += '</tr>'
}
}
}
elseif ($NoHeaders) {
if (!$Horizontal) {
foreach ($Object in $InputArray) {
if ($ErrorValues) {
for ($i = 1; $i -le $ErrorValues.count; $i++) {
if ($Object -imatch $ErrorValues[$i - 1]) {
$rowStart = '<tr class="error">'
break
}
elseif ($i -eq $errorValues.count) {
$rowStart = '<tr>'
}
}
}
else {
$rowStart = '<tr>'
}
$body += $rowStart
foreach ($Column in $ColumnsOrder) {
$Body += "<td $inlineCenter>" + $Object.($Column) + '</td>'
}
$body += '</tr>'
}
}
elseif ($Horizontal) {
foreach ($Column in $ColumnsOrder) {
$body += '<tr>'
foreach ($Object in $InputArray) {
$body += "<td $inlineCenter>" + $Object.($Column) + '</td>'
}
$body += '</tr>'
}
}
}
return $body + '</table>'
}