· 7 years ago · Nov 12, 2018, 02:08 PM
1##*===============================================
2##* FUNCTION DECLARATION
3##*===============================================
4Function Set-AlternatingRows {
5 <#
6 .SYNOPSIS
7 Simple function to alternate the row colors in an HTML table
8 .DESCRIPTION
9 This function accepts pipeline input from ConvertTo-HTML or any
10 string with HTML in it. It will then search for <tr> and replace
11 it with <tr class=(something)>. With the combination of CSS it
12 can set alternating colors on table rows.
13
14 CSS requirements:
15 .odd { background-color:#ffffff; }
16 .even { background-color:#dddddd; }
17
18 Classnames can be anything and are configurable when executing the
19 function. Colors can, of course, be set to your preference.
20
21 This function does not add CSS to your report, so you must provide
22 the style sheet, typically part of the ConvertTo-HTML cmdlet using
23 the -Head parameter.
24 .PARAMETER Line
25 String containing the HTML line, typically piped in through the
26 pipeline.
27 .PARAMETER CSSEvenClass
28 Define which CSS class is your "even" row and color.
29 .PARAMETER CSSOddClass
30 Define which CSS class is your "odd" row and color.
31 .EXAMPLE $Report | ConvertTo-HTML -Head $Header | Set-AlternateRows -CSSEvenClass even -CSSOddClass odd | Out-File HTMLReport.html
32
33 $Header can be defined with a here-string as:
34 $Header = @"
35 <style>
36 TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
37 TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
38 TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
39 .odd { background-color:#ffffff; }
40 .even { background-color:#dddddd; }
41 </style>
42 "@
43
44 This will produce a table with alternating white and grey rows. Custom CSS
45 is defined in the $Header string and included with the table thanks to the -Head
46 parameter in ConvertTo-HTML.
47 .NOTES
48 Author: Martin Pugh
49 Twitter: @thesurlyadm1n
50 Spiceworks: Martin9700
51 Blog: www.thesurlyadmin.com
52
53 Changelog:
54 1.1 Modified replace to include the <td> tag, as it was changing the class
55 for the TH row as well.
56 1.0 Initial function release
57 .LINK
58 http://community.spiceworks.com/scripts/show/1745-set-alternatingrows-function-modify-your-html-table-to-have-alternating-row-colors
59 .LINK
60 http://thesurlyadmin.com/2013/01/21/how-to-create-html-reports/
61 #>
62 [CmdletBinding()]
63 Param(
64 [Parameter(Mandatory,ValueFromPipeline)]
65 [string]$Line,
66
67 [Parameter(Mandatory)]
68 [string]$CSSEvenClass,
69
70 [Parameter(Mandatory)]
71 [string]$CSSOddClass
72 )
73 Begin {
74 $ClassName = $CSSEvenClass
75 }
76 Process {
77 If ($Line.Contains("<tr><td>"))
78 { $Line = $Line.Replace("<tr>","<tr class=""$ClassName"">")
79 If ($ClassName -eq $CSSEvenClass)
80 { $ClassName = $CSSOddClass
81 }
82 Else
83 { $ClassName = $CSSEvenClass
84 }
85 }
86 Return $Line
87 }
88}
89
90function Get-ActivationStatus {
91[CmdletBinding()]
92 param(
93 [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
94 [string]$DNSHostName = $Env:COMPUTERNAME
95 )
96 process {
97 try {
98 $wpa = Get-WmiObject SoftwareLicensingProduct -ComputerName $DNSHostName `
99 -Filter "ApplicationID = '55c92734-d682-4d71-983e-d6ec3f16059f'" `
100 -Property LicenseStatus -ErrorAction Stop
101 } catch {
102 $status = New-Object ComponentModel.Win32Exception ($_.Exception.ErrorCode)
103 $wpa = $null
104 }
105 $out = New-Object psobject -Property @{
106 ComputerName = $DNSHostName;
107 Status = [string]::Empty;
108 }
109 if ($wpa) {
110 :outer foreach($item in $wpa) {
111 switch ($item.LicenseStatus) {
112 0 {$out.Status = "Unlicensed"}
113 1 {$out.Status = "Licensed"; break outer}
114 2 {$out.Status = "Out-Of-Box Grace Period"; break outer}
115 3 {$out.Status = "Out-Of-Tolerance Grace Period"; break outer}
116 4 {$out.Status = "Non-Genuine Grace Period"; break outer}
117 5 {$out.Status = "Notification"; break outer}
118 6 {$out.Status = "Extended Grace"; break outer}
119 default {$out.Status = "Unknown value"}
120 }
121 }
122 } else {$out.Status = $status.Message}
123 $out
124 }
125}
126
127##*===============================================
128##* END FUNCTION DECLARATION
129##*===============================================
130
131#variables
132$Path = "C:\Temp\LogFiles\"
133$ComputerName = $env:computername
134$UserName = $env:USERNAME
135
136#Create path if doesn't exist
137If(!(test-path $Path))
138{
139 New-Item -ItemType Directory -Force -Path $Path
140}
141
142# Baseline of software to look for and to add to report
143## name must be exactly the same as displayed in control panel
144### add as many software as you like
145$BaselineSoftware = 'Adobe Acrobat Reader DC', `
146 'Citrix Receiver 4.12', `
147 'Java 8 Update 171', `
148 'Java 8 Update 171 (64-bit)', `
149 'Google Chrome', `
150 '7-Zip', `
151 'VLC media player', `
152 'Symantec Endpoint Protection', `
153 'Microsoft Office 365 ProPlus - en-us'
154
155$Device = Get-CimInstance Win32_ComputerSystem | Select Name, Model, Manufacturer, Domain
156
157# with adsisearcher there is no need for AD Powershell module
158$filter = "(&(objectCategory=computer)(objectClass=computer)(cn=$env:COMPUTERNAME))"
159$ouResult = (([adsisearcher]$filter).FindOne().Properties.distinguishedname).replace('CN=' + $env:COMPUTERNAME + ',','')
160$CompOU = $ouResult
161
162#Two registry keys where installed software is stored (depending on OS bit version)
163[string[]]$regKeyApplications = 'HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall','HKLM:SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
164
165#loop throug both registry keys (only if key exists) and store apps with a DisplayName
166[psobject[]]$regKeyApplication = @()
167ForEach ($regKey in $regKeyApplications) {
168 If (Test-Path -LiteralPath $regKey -ErrorAction 'SilentlyContinue') {
169 [psobject[]]$UninstallKeyApps = Get-ChildItem -LiteralPath $regKey -ErrorAction 'SilentlyContinue'
170 ForEach ($UninstallKeyApp in $UninstallKeyApps) {
171 Try {
172 [psobject]$regKeyApplicationProps = Get-ItemProperty -LiteralPath $UninstallKeyApp.PSPath -ErrorAction 'Stop'
173 If ($regKeyApplicationProps.DisplayName) { [psobject[]]$regKeyApplication += $regKeyApplicationProps }
174 }
175 Catch{
176 Continue
177 }
178 }
179 }
180 }
181
182$Act_Status = Get-ActivationStatus | select -ExpandProperty Status
183
184#create report
185$Report = @()
186foreach ($Software in $BaselineSoftware) {
187
188 $ObjProperties = New-Object PSObject
189 $App = $regKeyApplication | where {$_.DisplayName -eq $Software}
190
191 $DisplayName = $Software
192 $Version = if ($App.DisplayVersion -is [system.array]) {$App.DisplayVersion[0]}else{[string]$App.DisplayVersion}
193 $Publisher = if ($App.Publisher -is [system.array]) {$App.Publisher[0]}else{[string]$App.Publisher}
194
195 Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Software" -Value $DisplayName
196 Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Version" -Value $Version
197 Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Publisher" -Value $Publisher
198
199 if ($App) {
200 Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Installation" -Value "Success"
201 } else {
202 Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Installation" -Value "Failed"
203 }
204
205 $Report += $ObjProperties
206}
207$Report = $Report | Sort-Object -Property Installation,Software
208
209#Define first part of HTML document
210$Header = @"
211<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
212<html xmlns="http://www.w3.org/1999/xhtml">
213<head>
214<style>
215BODY {font-family: Calibri, Arial, Sans-serif;}
216TABLE {border: 1px solid #0d367d;border-collapse: collapse;text-align: left;}
217TH {border-bottom: 1px solid #fff;color:#fff;padding: 3px;background-color: #0d367d;}
218TD {border-bottom: 1px solid #fff;padding: 3px;}
219.odd { background-color:#dae5f4; }
220.even { background-color:#b8d1f3; }
221h1 {
222 font-size: 38px;
223 font-weight: bold;
224 text-decoration: underline;
225
226}
227
228.pborder
229{
230 border-style:double;
231 border-color:green;
232 display:inline;
233 border-width:3px;
234 font-weight: bold;
235 color:green;
236 padding-left:5px;
237 padding-right:5px;
238}
239</style>
240<title>
241Software Inventory after OS Deployment
242</title>
243</head><body>
244"@
245
246#Define last part of HTML document
247$Footer = "</body></html>"
248
249$Pre = "The Report is generated on $(Get-Date -format "dd/MM/yyyy HH:mm:ss") on computer <b>$env:Computername</b> during OS Deployment."
250$Post = "Install missing software!"
251
252$Prebody = @()
253$Prebody += "<h1> OSD Software Installation Log </h1>"
254$Prebody += ""
255
256if ($Act_Status -eq "Licensed") {
257 $Prebody += "<u>Windows Activation Status: <font size='4' face='Calibri' color='#0B610B'><b>Licensed</b></font>.</u><br><br>"
258
259} else {
260 $Prebody += "<u><font size='4' face='Calibri' color='#FF0000'>Windows Activation Status: <b>Unlicensed</b>.</u><br>"
261 $Prebody += "<b>Make sure Windows is activated before it's handed out to user!</b></font><br><br>"
262}
263
264$Prebody += "<b><u>$ComputerName</u> is located in OU:</b> <br>"
265$Prebody += "$CompOU<br><br>"
266
267$Afterbody = @()
268$Afterbody += "<br><br>"
269$Afterbody += "<br><br>Script ran with user: $UserName"
270
271$HTMLReport = $Report | ConvertTo-Html -Fragment -PreContent $Pre -PostContent $Post | Set-AlternatingRows -CSSEvenClass even -CSSOddClass odd
272
273#Construct HTML body
274$body =@()
275$body += $Header
276$body += $Prebody
277$body += $HTMLReport
278$body += $Afterbody
279$body += $Footer
280
281#Define color table for Failed and Successful installations
282$colorTagTable = @{Failed = ' bgcolor="red"><b>Failed</b><';
283 Success = ' bgcolor="green"><b>Success</b><'}
284
285#get possible values and search them in text sorrounded by > < and replace them with style
286$colorTagTable.Keys | foreach { $body = $body -replace ">$_<",($colorTagTable.$_) }
287
288#Export HTML Report
289$body | Out-File "$Path\$ComputerName.html"
290
291# create runonce reg key (report will open automatically if logged in with Admin account)
292$RunOnceKey = "HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce"
293Set-ItemProperty $RunOnceKey "InstallLog" ("C:\Program Files\Internet Explorer\iexplore.exe $Path\$ComputerName.html")