-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathConvert-DataTableToHtmlTable.ps1
215 lines (188 loc) · 5.83 KB
/
Convert-DataTableToHtmlTable.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
function Convert-DataTableToHtmlTable
{
<#
.SYNOPSIS
This function can be used to convert a data table or ps object into a html table.
.PARAMETER $DataTable
The datatable to input.
.PARAMETER $Outfile
The output file path.
.PARAMETER $Title
Title of the page.
.PARAMETER $Description
Description of the page.
.EXAMPLE
$object = New-Object psobject
$Object | Add-Member Noteproperty Name "my name 1"
$Object | Add-Member Noteproperty Description "my description 1"
Convert-DataTableToHtmlTable -Verbose -DataTable $object -Outfile ".\MyPsDataTable.html" -Title "MyPage" -Description "My description" -DontExport
Convert-DataTableToHtmlTable -Verbose -DataTable $object -Outfile ".\MyPsDataTable.html" -Title "MyPage" -Description "My description"
.\MyPsDataTable.html
.NOTES
Author: Scott Sutherland (@_nullbind)
#>
param (
[Parameter(Mandatory = $true,
HelpMessage = 'The datatable to input.')]
$DataTable,
[Parameter(Mandatory = $false,
HelpMessage = 'The output file path.')]
[string]$Outfile = ".\MyPsDataTable.html",
[Parameter(Mandatory = $false,
HelpMessage = 'Title of page.')]
[string]$Title = "HTML Table",
[Parameter(Mandatory = $false,
HelpMessage = 'Description of page.')]
[string]$Description = "HTML Table",
[Parameter(Mandatory = $false,
HelpMessage = 'Disable file save.')]
[switch]$DontExport
)
# Setup HTML begin
Write-Verbose "[+] Creating html top."
$HTMLSTART = @"
<html>
<head>
<title>$Title</title>
<style>
{box-sizing:border-box}
body,html{
font-family:"Open Sans",
sans-serif;font-weight:400;
min-height:100%;;color:#3d3935;
margin:1px;line-height:1.5;
}
table{
width:100%;
max-width:100%;
margin-bottom:1rem;
border-collapse:collapse
}
table thead th{
vertical-align:bottom;
border-bottom:2px solid #eceeef
}
table tbody tr:nth-of-type(odd){
background-color:#f9f9f9
}
table tbody tr:hover{
background-color:#f5f5f5
}
table td,table th{
padding:.75rem;
line-height:1.5;
text-align:left;
font-size:1rem;
vertical-align:top;
border-top:1px solid #eceeef
}
h1,h2,h3,h4,h5,h6{
padding-left: 10px;
font-family:inherit;
font-weight:500;
line-height:1.1;
color:inherit
}
code{
padding:.2rem .4rem;
font-size:1rem;
color:#bd4147;
background-color:#f7f7f9;
border-radius:.25rem
}
p{
margin-top:0;
margin-bottom:1rem
}
a,a:visited{
text-decoration:none;
font-size: 14;
color: gray;
font-weight: bold;
}
a:hover{
color:#9B3722;
text-decoration:underline
}
.link:hover{
text-decoration:underline
}
li{
list-style-type:none
}
.pageDescription {
margin: 10px;
}
.divbarDomain{
background:#d9d7d7;
width:200px;
border: 1px solid #999999;
height: 25px;
text-align:center;
}
.divbarDomainInside{
background:#9B3722;
width:100px;
text-align:center;
height: 25px;
vertical-align:middle;
}
.pageDescription {
padding-left: 0px;
}
</style>
</head>
<body>
<p class="pageDescription"><a href="javascript:history.back()">Back to Report</a></p>
<p class="pageDescription"><h3>$Title</h3></p>
<p class="pageDescription">$Description</p>
<table class="table table-striped table-hover">
"@
# Get list of columns
Write-Verbose "[+] Parsing data table columns."
$MyCsvColumns = $DataTable | Get-Member | Where-Object MemberType -like "NoteProperty" | Select-Object Name -ExpandProperty Name
# Print columns creation
Write-Verbose "[+] Creating html table columns."
$HTMLTableHeadStart= "<thead><tr>"
$MyCsvColumns |
ForEach-Object {
# Add column
$HTMLTableColumn = "<th>$_</th>$HTMLTableColumn"
}
$HTMLTableColumn = "$HTMLTableHeadStart$HTMLTableColumn</tr></thead>"
# Create table rows
Write-Verbose "[+] Creating html table rows."
$HTMLTableRow = $DataTable |
ForEach-Object {
# Create a value contain row data
$CurrentRow = $_
$PrintRow = ""
$MyCsvColumns |
ForEach-Object{
$GetValue = $CurrentRow | Select-Object $_ -ExpandProperty $_
if($PrintRow -eq ""){
#$PrintRow = "<td>$GetValue</td>"
}else{
$PrintRow = "<td>$GetValue</td>$PrintRow"
}
}
# Return row
$HTMLTableHeadstart = "<tr>"
$HTMLTableHeadend = "</tr>"
"$HTMLTableHeadStart$PrintRow$HTMLTableHeadend"
}
# Setup HTML end
Write-Verbose "[+] Creating html bottom."
$HTMLEND = @"
</tbody>
</table>
</body>
</html>
"@
# Return it
"$HTMLSTART $HTMLTableColumn $HTMLTableRow $HTMLEND"
# Write file
if(-not $DontExport){
"$HTMLSTART $HTMLTableColumn $HTMLTableRow $HTMLEND" | Out-File $Outfile
}
}