-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-PSVersion-MultiTable.ps1
281 lines (232 loc) · 13.2 KB
/
Get-PSVersion-MultiTable.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
#Region [ Script Paramters ]
param(
[Parameter(Mandatory=$false)]
[ValidateSet('None','Audit')]
[String]$Audit = 'None',
[Parameter(Mandatory=$false)]
[ValidateSet('All','CSV','HTML')]
[String]$Report = 'CSV',
[Parameter(Mandatory=$false)]
[Switch]$ShowReport
)
#EndRegion [ Script Paramters ]
#Region [ PowerShell Function ]
''
Write-Output "START - $(Get-Date -Format yyyy-MM-dd-HH:mm)"
''
Write-Output " - (1/3) - $(Get-Date -Format MM-dd-HH:mm) - Gathering PowerShell Versions"
Function Get-PSVersion{
<#
.SYNOPSIS
Provides PowerShell version information
.Description
Provides both Both PowerShell and PowerShell Core version information
.Link
https://celerium.org
.EXAMPLE
Get-PSVersion
.Notes
This is a proof of concept script to see If I could create better HTML reports
#>
Try{
$PSInfo = $PSVersionTable
$PowerShell7 = (Test-Path -Path "C:\Program Files\PowerShell\7\pwsh.exe")
$OSInformation = Get-CimInstance -ClassName Win32_Operatingsystem
$ParameterSplat =
@{Name='Computer';Expression={($OSInformation).CSName}}, `
@{Name='OperatingSystem';Expression={($OSInformation).Caption}}, `
@{Name='PSVersion';Expression={($_.PSVersion).ToString()}}, `
@{Name='PSCLRVersion';Expression={If($Null -eq $_.CLRVersion){([System.Reflection.Assembly]::GetExecutingAssembly().ImageRuntimeVersion).Replace("v","")}Else{($_.CLRVersion).ToString()}}}, `
@{Name='PSWSManStackVersion';Expression={($_.WSManStackVersion).ToString()}}, `
@{Name='PSRemotingProtocolVersion';Expression={($_.PSRemotingProtocolVersion).ToString()}}, `
@{Name='PSSerializationVersion';Expression={($_.SerializationVersion).ToString()}}, `
@{Name='PSBuildVersion';Expression={If($Null -eq $_.BuildVersion){'N\A'}Else{($_.BuildVersion).ToString()}}}, `
@{Name='PSEdition';Expression={$_.PSEdition}}, `
@{Name='PSOS';Expression={If($Null -eq $_.OS){(($OSInformation).Caption -replace "(?<=Microsoft Windows).*")+" "+($OSInformation).Version}Else{$_.OS}}}, ` #positive reverse lookup
@{Name='PSPlatform';Expression={If($Null -eq $_.Platform){"Win32NT"}Else{$_.Platform}}},`
@{Name='PSCompatibleVersions';Expression={$_.PSCompatibleVersions -Join(',')}}
If ($PowerShell7 -eq $False){
#PSVersion 1-5
$PSInfo | Select-Object $ParameterSplat
}
ElseIf($PowerShell7 -eq $True){
#PSVersion 1-5
$PSInfo = powershell.exe -nologo -noprofile -command {$PSVersionTable}
$PSInfo | Select-Object $ParameterSplat
#PSVersion 7+
$PSInfo = pwsh -nologo -noprofile -command {$PSVersionTable}
$PSInfo | Select-Object $ParameterSplat
}
Else{$PSVersionTable | Select-Object $ParameterSplat
}
}
Catch{
$ErrorMessage = $_ | Out-String
Write-Host ($ErrorMessage).Trim() -ForegroundColor Red -BackgroundColor Black
}
Finally{
#Future Use
}
}
#EndRegion [ PowerShell Function ]
#Region [ Report\Script Variables ]
$PSVersion = Get-PSVersion
#$ScriptName = $MyInvocation.MyCommand.Name
$ScriptName = 'Get-PSVersion'
$ReportFolderName = "$ScriptName-Report"
$FileDate = Get-Date -Format 'yyyy-MM-dd-HHmm'
$HTMLDate = (Get-Date -Format 'yyyy-MM-dd h:mmtt').ToLower()
$FQDN = ((Get-CimInstance -ClassName Win32_ComputerSystem).Domain).Split('.')[0]
$ShortFQDN = ($FQDN).Split('.')[0]
$DomainController = ($env:LOGONSERVER).Replace('\\','')
#Define Logging Location
Try{
If ($Audit -eq 'None'){
$Log = "C:\Audits\Logs\$ReportFolderName"
}
If ($Audit -eq 'Audit'){
$Log = "C:\Audits\Logs\$ShortFQDN-$Audit-Audit-Reports\$ReportFolderName"
}
}
Catch{
$ErrorMessage = $_ | Out-String
Write-Host ($ErrorMessage).Trim() -ForegroundColor Red -BackgroundColor Black
break
}
#Create Logging Location
Try{
If (Test-Path -Path $Log -PathType Container){$Null}
Else{
New-Item -Path $Log -ItemType Directory | Out-Null
}
}
Catch{
$ErrorMessage = $_ | Out-String
Write-Host ($ErrorMessage).Trim() -ForegroundColor Red -BackgroundColor Black
break
}
#Log Names
$CSVReport = "$Log\$ShortFQDN-$ScriptName-Report-$($FileDate).csv"
$HTMLReport = "$Log\$ShortFQDN-$ScriptName-Report-$($FileDate).html"
#EndRegion [ Report\Script Variables ]
#Region [ CSV Report ]
Try{
If($Report -eq 'All' -or $Report -eq 'CSV'){
Write-Output " - (2/3) - $(Get-Date -Format MM-dd-HH:mm) - Generating CSV"
$PSVersion | Sort-Object Computer | Select-Object $Scriptname,* | Export-Csv $CSVReport -NoTypeInformation
}
}
Catch{
$ErrorMessage = $_ | Out-String
Write-Host ($ErrorMessage).Trim() -ForegroundColor Red -BackgroundColor Black
break
}
#EndRegion [ CSV Report ]
#Region [ HTML Report]
Try{
If($Report -eq 'All' -or $Report -eq 'HTML'){
Write-Output " - (3/2) - $(Get-Date -Format MM-dd-HH:mm) - Generating HTML"
#Region [ HTML Report Building Blocks ]
# Build the HTML header
# This grabs the raw text from files to shorten the amount of lines in the PSScript
# General idea is that the HTML assets would infrequently be changed once set
$Meta = Get-Content -Path "$PSScriptRoot\Assets\Meta.html" -Raw
$Meta = $Meta -replace 'xTITLECHANGEx',"$ScriptName"
$CSS = Get-Content -Path "$PSScriptRoot\Assets\Styles.css" -Raw
$JavaScript = Get-Content -Path "$PSScriptRoot\Assets\JavaScriptHeader.html" -Raw
$Head = $Meta + ("<style>`n") + $CSS + ("`n</style>") + $JavaScript
# HTML Body Building Blocks (In order)
$TopNav = Get-Content -Path "$PSScriptRoot\Assets\TopBar.html" -Raw
$DivMainStart = '<div id="layoutSidenav">'
$SideBar = Get-Content -Path "$PSScriptRoot\Assets\SideBar.html" -Raw
$SideBar = $SideBar -replace ('xTIMESETx',"$HTMLDate")
$DivSecondStart = '<div id="layoutSidenav_content">'
$PreLoader = Get-Content -Path "$PSScriptRoot\Assets\PreLoader.html" -Raw
$MainStart = '<main>'
#Base Table Container
$BaseTableContainer = Get-Content -Path "$PSScriptRoot\Assets\TableContainer.html" -Raw
#Summary Header
$SummaryTableContainer = $BaseTableContainer
$SummaryTableContainer = $SummaryTableContainer -replace ('xHEADERx',"$ScriptName - Summary")
$SummaryTableContainer = $SummaryTableContainer -replace ('xBreadCrumbx',"Data gathered from $DomainController")
#Summary Cards
#HTML in Summary.html would be edited depending on the report and summary info you want to show
$SummaryCards = Get-Content -Path "$PSScriptRoot\Assets\Summary.html" -Raw
$SummaryCards = $SummaryCards -replace ('xCARD1Valuex','$100.00')
$SummaryCards = $SummaryCards -replace ('xCARD2Valuex','$125,525.00')
$SummaryCards = $SummaryCards -replace ('xCARD3Valuex','80%')
#Body table headers, would be duplicated\adjusted depending on how many tables you want to show
$BodyTableContainer = $BaseTableContainer
$BodyTableContainer = $BodyTableContainer -replace ('xHEADERx',"$ScriptName - Details")
$BodyTableContainer = $BodyTableContainer -replace ('xBreadCrumbx',"Data gathered from $DomainController")
#Ending HTML
$DivEnd = '</div>'
$MainEnd = '</main>'
$JavaScriptEnd = Get-Content -Path "$PSScriptRoot\Assets\JavaScriptEnd.html" -Raw
#EndRegion [ HTML Report Building Blocks ]
#Region [ Example HTML Report Data\Structure ]
#Temp data filler to simulate large tables of data
#Used just an an example
$PSVersion = [Array]$PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion +
$PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion +
$PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion +
$PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion +
$PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion + $PSVersion
#Creates an HTML table from PowerShell function results without any extra HTML tags
$TableResults = $PSVersion | ConvertTo-Html -As Table -Fragment -Property Computer,OperatingSystem,PSVersion,PSBuildVersion,PSEdition,PSOS `
-PostContent ' <ul>
<li>Note: SAMPLE 1 = Only applies stuff and things</li>
<li>Note: SAMPLE 2 = Only applies stuff and things</li>
<li>Note: SAMPLE 3 = Only applies stuff and things</li>
</ul>
'
#Table section segragation
#PS doesnt create a <thead> tag so I have find the first row and make it so
$TableHeader = $TableResults -split "`r`n" | Where-Object {$_ -match '<th>'}
#Unsure why PS makes empty <colgroup> as it contains no data
$TableColumnGroup = $TableResults -split "`r`n" | Where-Object {$_ -match '<colgroup>'}
#Table ModIfications
#Replacing empty html table tags with simple replacable names
#It was annoying me that empty rows showed in the raw HTML and I couldnt delete them as they were not $NUll but were empty
$TableResults = $TableResults -replace ($TableHeader,'xblanklinex')
$TableResults = $TableResults -replace ($TableColumnGroup,'xblanklinex')
$TableResults = $TableResults | Where-Object {$_ -ne 'xblanklinex'} | ForEach-Object {$_.Replace('xblanklinex','')}
#Inject Modifyied data back into the table
#Makes the table have a <thead> tag
$TableResults = $TableResults -replace '<Table>',"<Table>`n<thead>$TableHeader</thead>"
$TableResults = $TableResults -replace '<table>','<table class="dataTable-table" style="width: 100%;">'
#Mark Focus Data to draw attention\talking points
#Need to understand RegEx more as this doesnt scale at all
$TableResults = $TableResults -replace '<td>7.1.4</td>','<td class="GoodStatus">7.1.4</td>'
$TableResults = $TableResults -replace '<td>5.1.19041.1237</td>','<td class="BadStatus">5.1.19041.1237</td>'
$TableResults = $TableResults -replace '<td>N\\A</td>','<td class="WarningStatus">N\A</td>' # RegEx \ to escape the other \
$TableResults = $TableResults -replace '<td>Core</td>','<td class="InfoStatus">Core</td>'
#Building the final HTML report using the various ordered HTML building blocks from above.
#This is injecting html\css\javascript in a certain order into a file to make an HTML report
$HTML = ConvertTo-HTML -Head $Head -Body " $TopNav $DivMainStart $SideBar $DivSecondStart $PreLoader $MainStart
$SummaryTableContainer $SummaryCards $DivEnd $DivEnd $DivEnd
$BodyTableContainer $TableResults $DivEnd $DivEnd $DivEnd
$BodyTableContainer $TableResults $DivEnd $DivEnd $DivEnd
$BodyTableContainer $TableResults $DivEnd $DivEnd $DivEnd
$MainEnd $DivEnd $DivEnd $JavaScriptEnd
"
$HTML = $HTML -replace '<body>','<body class="sb-nav-fixed">'
$HTML | Out-File $HTMLReport -Encoding utf8
}
}
Catch{
$ErrorMessage = $_ | Out-String
Write-Host ($ErrorMessage).Trim() -ForegroundColor Red -BackgroundColor Black
break
}
#EndRegion [ Example HTML Report Data\Structure ]
#EndRegion [ HTML Report ]
#Region [ Show Report]
#Open File Explorer to show log output
If ($ShowReport){
Invoke-Item $Log
}
''
Write-Output "END - $(Get-Date -Format yyyy-MM-dd-HH:mm)"
''
#EndRegion [ Show Report]