Answered by:
Windows update HTML Report

Question
-
I am new to Powershell HTML reports. i want to try but cant seem to wrap my head around what i am doing wrong. i basically want to take a list of servers, check the last reboot time, see the list of patches that were installed, and what services are not running. i basically can get the info out for multiple but not sure why i cant get them in the same table, example the Server Reboot Report will have 2 tables with different servers. i am not sure what is needed to make them line up under each heading.
$smtpsettings = @{ SmtpServer = "192.168.1.100" From = "WindowsUpdateReport@abc.com" To = "ServerAdmins@abc.com" Subject = "Windows Update Patch Report" } $pdate =((Get-Date).addDays(-30).ToshortdateString()) $PC = Get-Content 'C:\Scripts\Jobs\PC-list2.txt' $head = @" <Title>Hot Fix Report</Title> <style> body { background-color:#FFFFFF; font-family:Tahoma; font-size:12pt; } td, th { border:1px solid black; border-collapse:collapse; } th { color:white; background-color:black; } table, tr, td, th { padding: 2px; margin: 0px } tr:nth-child(odd) {background-color: lightgray} table { width:95%;margin-left:5px; margin-bottom:20px;} </style> <br> <H1>Hot Fix Report</H1> "@ $head2 = @" <Title>Services Not Running Report </Title> <style> body { background-color:#FFFFFF; font-family:Tahoma; font-size:12pt; } td, th { border:1px solid black; border-collapse:collapse; } th { color:white; background-color:black; } table, tr, td, th { padding: 2px; margin: 0px } tr:nth-child(odd) {background-color: lightgray} table { width:95%;margin-left:5px; margin-bottom:20px;} </style> <br> <H1>Services Not Running Report</H1> "@ $head3 = @" <Title>Server Reboot Report</Title> <style> body { background-color:#FFFFFF; font-family:Tahoma; font-size:12pt; } td, th { border:1px solid black; border-collapse:collapse; } th { color:white; background-color:black; } table, tr, td, th { padding: 2px; margin: 0px } tr:nth-child(odd) {background-color: lightgray} table { width:95%;margin-left:5px; margin-bottom:20px;} </style> <br> <H1>Server Reboot Report</H1> "@ $head4 = @" <Title>Failed Windows Updates Report</Title> <style> body { background-color:#FFFFFF; font-family:Tahoma; font-size:12pt; } td, th { border:1px solid black; border-collapse:collapse; } th { color:white; background-color:black; } table, tr, td, th { padding: 2px; margin: 0px } tr:nth-child(odd) {background-color: lightgray} table { width:95%;margin-left:5px; margin-bottom:20px;} </style> <br> <H1>Failed Windows Updates Report</H1> "@ $paramHash1 = @{ Head = $head Title = "Hot Fix Report" As = "Table" PostContent = "<H6>$(Get-Date)</H6>" Property = "PSComputerName","HotFixID","Description", "InstalledOn","Caption" } $paramHash2 = @{ Head = $head2 Title = "Services Not Running Report" As = "Table" PostContent = "<H6>$(Get-Date)</H6>" Property = "PSComputerName","Caption", "State" } $paramHash3 = @{ Head = $head3 Title = "Server Reboot Report" As = "Table" PostContent = "<H6>$(Get-Date)</H6>" Property = "MachineName","Message", "TimeCreated" } $paramHash4 = @{ Head = $head4 Title = "Failed Windows Updates Report" As = "Table" PostContent = "<H6>$(Get-Date)</H6>" Property = "MachineName","Message", "TimeWritten" } $Data2 ="" $SRVReboot2 = "" $ServiceCheck2 = "" $Report = Foreach ($a in $pc) { $SRVReboot = get-winevent -ComputerName $a -FilterHashtable @{Logname='system';ID=6013} -MaxEvents 1 | Select-Object machinename, Message,TimeCreated $data = Get-HotFix -ComputerName $a | ? installedon -gt $pdate |Select-Object PSComputerName, Caption, Description, hotfixid, installedon, name $ServiceCheck = Get-WmiObject Win32_Service -ComputerName $a |Where-Object { $_.StartMode -eq 'Auto' -and $_.State -ne 'Running' } |Select-Object PSComputerName, Caption, Name, State $SRVReboot2 += $SRVReboot | ConvertTo-Html @paramHash3 $ServiceCheck2 += $ServiceCheck | ConvertTo-Html @paramHash2 $Data2 += $data | ConvertTo-Html @paramHash1 } #$SRVReboot2 = $SRVReboot2 | ConvertTo-Html @paramHash3 #$ServiceCheck2 = $ServiceCheck2 | ConvertTo-Html @paramHash2 #$Data2 = $Data2 | ConvertTo-Html @paramHash1 $Report = $data2 + $ServiceCheck2 + $SRVReboot2 $Report #$Message = $Report Send-MailMessage @smtpsettings -Body "$Report" -BodyAsHtml
- Edited by Jeff_at_ZF Monday, January 15, 2018 7:07 PM better formatting
Sunday, January 14, 2018 8:44 PM
Answers
-
Some guidance. First gather all of the data into a collection. Once you have a collection of data then attempt to create an HTML report. Separate the two task completely.
To get all of what you need design a single custom object to hold all of the information per server. This can then be enumerated into a report.
By separating these you can concentrate on getting the data and test and adjust to what you need. Turning it into a report is now as simple as Running ConvertTo-Html. If you want multiple tables just wrap each bit in <div> tags for table.
$tbl1 = $collection | Where{$_.Type -eq 'Uptime'} | ConvertTo-Html -Fragment.
$tbl2 = $collection | Where{$_.Type -eq 'Down'} | ConvertTo-Html -Fragment.Then combine all fragments with ConvertTo-Html
Look in the Gallery for numerous examples.
\_(ツ)_/
- Marked as answer by Jeff_at_ZF Tuesday, January 16, 2018 1:34 PM
Sunday, January 14, 2018 9:00 PM -
I got it to finally work. I had to use arrays and get it info out of it to do it, but it finally works! If you would like to use it please do.
$smtpsettings = @{ SmtpServer = "192.168.1.100" From = "WindowsUpdateReport@abc.com" To = "ServerAdmins@abc.com" Subject = "Windows Update Patch Report" } $pdate =((Get-Date).addDays(-30).ToshortdateString()) $PC = Get-Content 'C:\Scripts\Jobs\PC-list2.txt' $head = @" <Title>Hot Fix Report</Title> <style> body { background-color:#FFFFFF; font-family:Tahoma; font-size:12pt; } td, th { border:1px solid black; border-collapse:collapse; } th { color:white; background-color:black; } table, tr, td, th { padding: 2px; margin: 0px } tr:nth-child(odd) {background-color: lightgray} table { width:95%;margin-left:5px; margin-bottom:20px;} </style> <br> <H1>Hot Fix Report</H1> "@ $head2 = @" <Title>Services Not Running Report </Title> <style> body { background-color:#FFFFFF; font-family:Tahoma; font-size:12pt; } td, th { border:1px solid black; border-collapse:collapse; } th { color:white; background-color:black; } table, tr, td, th { padding: 2px; margin: 0px } tr:nth-child(odd) {background-color: lightgray} table { width:95%;margin-left:5px; margin-bottom:20px;} </style> <br> <H1>Services Not Running Report</H1> "@ $head3 = @" <Title>Server Reboot Report</Title> <style> body { background-color:#FFFFFF; font-family:Tahoma; font-size:12pt; } td, th { border:1px solid black; border-collapse:collapse; } th { color:white; background-color:black; } table, tr, td, th { padding: 2px; margin: 0px } tr:nth-child(odd) {background-color: lightgray} table { width:95%;margin-left:5px; margin-bottom:20px;} </style> <br> <H1>Server Reboot Report</H1> "@ $head4 = @" <Title>Failed Windows Updates Report</Title> <style> body { background-color:#FFFFFF; font-family:Tahoma; font-size:12pt; } td, th { border:1px solid black; border-collapse:collapse; } th { color:white; background-color:black; } table, tr, td, th { padding: 2px; margin: 0px } tr:nth-child(odd) {background-color: lightgray} table { width:95%;margin-left:5px; margin-bottom:20px;} </style> <br> <H1>Failed Windows Updates Report</H1> "@ $paramHash1 = @{ Head = $head Title = "Hot Fix Report" As = "Table" PostContent = "<H6>$(Get-Date)</H6>" Property = "ComputerName","HotFixID","Description", "InstalledOn","Caption" } $paramHash2 = @{ Head = $head2 Title = "Services Not Running Report" As = "Table" PostContent = "<H6>$(Get-Date)</H6>" Property = "ComputerName","Caption", "State" } $paramHash3 = @{ Head = $head3 Title = "Server Reboot Report" As = "Table" PostContent = "<H6>$(Get-Date)</H6>" Property = "ComputerName","Message", "TimeCreated" } $paramHash4 = @{ Head = $head4 Title = "Failed Windows Updates Report" As = "Table" PostContent = "<H6>$(Get-Date)</H6>" Property = "MachineName","Message", "TimeWritten" } $Data2 = @() $SRVReboot2 = @() $ServiceCheck2 = @() $Report = Foreach ($a in $pc) { $SRVReboot = get-winevent -ComputerName $a -FilterHashtable @{Logname='system';ID=6013} -MaxEvents 1 | Select-Object machinename, Message,TimeCreated foreach ($srvr in $SRVReboot) { $SRVRebootItem = new-object PSObject $SRVRebootItem | add-member NoteProperty "Computername" $srvr.machinename $SRVRebootItem | add-member NoteProperty "Message" $srvr.Message $SRVRebootItem | add-member NoteProperty "TimeCreated" $srvr.TimeCreated $SRVReboot2 += $SRVRebootItem } $data = Get-HotFix -ComputerName $a | ? installedon -gt $pdate | Select-Object PSComputerName, Caption, Description, hotfixid, installedon, name foreach ($d in $data) { $dataItem = new-object PSObject $dataItem | add-member NoteProperty "ComputerName" $d.PSComputerName $dataItem | add-member NoteProperty "Caption" $d.Caption $dataItem | add-member NoteProperty "Description" $d.Description $dataItem | add-member NoteProperty "hotfixid" $d.hotfixid $dataItem | add-member NoteProperty "installedon" $d.installedon $dataItem | add-member NoteProperty "name" $d.name $Data2 += $dataItem } $ServiceCheck = Get-WmiObject Win32_Service -ComputerName $a |Where-Object { $_.StartMode -eq 'Auto' -and $_.State -ne 'Running' } |Select-Object PSComputerName, Caption, Name, State foreach ($sc in $ServiceCheck) { $ServiceCheckItem = new-object PSObject $ServiceCheckItem | add-member NoteProperty "ComputerName" $sc.PSComputerName $ServiceCheckItem | add-member NoteProperty "Caption" $sc.Caption $ServiceCheckItem | add-member NoteProperty "Name" $sc.Name $ServiceCheckItem | add-member NoteProperty "State" $sc.State $ServiceCheck2 += $ServiceCheckItem } } $SRVReboot2 = $SRVReboot2 | ConvertTo-Html @paramHash3 $ServiceCheck2 = $ServiceCheck2 | ConvertTo-Html @paramHash2 $Data2 = $Data2 | ConvertTo-Html @paramHash1 $Report = $data2 + $ServiceCheck2 + $SRVReboot2 Send-MailMessage @smtpsettings -Body "$Report" -BodyAsHtml
- Marked as answer by Jeff_at_ZF Tuesday, January 16, 2018 1:34 PM
Tuesday, January 16, 2018 1:33 PM
All replies
-
You must post scripts using the posting tool on the edit bar. Please edit your original post and place the code into the code posting control.
We cannot spend time trying to figure out your code. Post a simple example of what is not working and any error messages.
Please review the following for guidance.
This Forum is for Scripting Question Rather than script requests
\_(ツ)_/
- Edited by jrv Sunday, January 14, 2018 8:51 PM
Sunday, January 14, 2018 8:49 PM -
Some guidance. First gather all of the data into a collection. Once you have a collection of data then attempt to create an HTML report. Separate the two task completely.
To get all of what you need design a single custom object to hold all of the information per server. This can then be enumerated into a report.
By separating these you can concentrate on getting the data and test and adjust to what you need. Turning it into a report is now as simple as Running ConvertTo-Html. If you want multiple tables just wrap each bit in <div> tags for table.
$tbl1 = $collection | Where{$_.Type -eq 'Uptime'} | ConvertTo-Html -Fragment.
$tbl2 = $collection | Where{$_.Type -eq 'Down'} | ConvertTo-Html -Fragment.Then combine all fragments with ConvertTo-Html
Look in the Gallery for numerous examples.
\_(ツ)_/
- Marked as answer by Jeff_at_ZF Tuesday, January 16, 2018 1:34 PM
Sunday, January 14, 2018 9:00 PM -
I got it to finally work. I had to use arrays and get it info out of it to do it, but it finally works! If you would like to use it please do.
$smtpsettings = @{ SmtpServer = "192.168.1.100" From = "WindowsUpdateReport@abc.com" To = "ServerAdmins@abc.com" Subject = "Windows Update Patch Report" } $pdate =((Get-Date).addDays(-30).ToshortdateString()) $PC = Get-Content 'C:\Scripts\Jobs\PC-list2.txt' $head = @" <Title>Hot Fix Report</Title> <style> body { background-color:#FFFFFF; font-family:Tahoma; font-size:12pt; } td, th { border:1px solid black; border-collapse:collapse; } th { color:white; background-color:black; } table, tr, td, th { padding: 2px; margin: 0px } tr:nth-child(odd) {background-color: lightgray} table { width:95%;margin-left:5px; margin-bottom:20px;} </style> <br> <H1>Hot Fix Report</H1> "@ $head2 = @" <Title>Services Not Running Report </Title> <style> body { background-color:#FFFFFF; font-family:Tahoma; font-size:12pt; } td, th { border:1px solid black; border-collapse:collapse; } th { color:white; background-color:black; } table, tr, td, th { padding: 2px; margin: 0px } tr:nth-child(odd) {background-color: lightgray} table { width:95%;margin-left:5px; margin-bottom:20px;} </style> <br> <H1>Services Not Running Report</H1> "@ $head3 = @" <Title>Server Reboot Report</Title> <style> body { background-color:#FFFFFF; font-family:Tahoma; font-size:12pt; } td, th { border:1px solid black; border-collapse:collapse; } th { color:white; background-color:black; } table, tr, td, th { padding: 2px; margin: 0px } tr:nth-child(odd) {background-color: lightgray} table { width:95%;margin-left:5px; margin-bottom:20px;} </style> <br> <H1>Server Reboot Report</H1> "@ $head4 = @" <Title>Failed Windows Updates Report</Title> <style> body { background-color:#FFFFFF; font-family:Tahoma; font-size:12pt; } td, th { border:1px solid black; border-collapse:collapse; } th { color:white; background-color:black; } table, tr, td, th { padding: 2px; margin: 0px } tr:nth-child(odd) {background-color: lightgray} table { width:95%;margin-left:5px; margin-bottom:20px;} </style> <br> <H1>Failed Windows Updates Report</H1> "@ $paramHash1 = @{ Head = $head Title = "Hot Fix Report" As = "Table" PostContent = "<H6>$(Get-Date)</H6>" Property = "ComputerName","HotFixID","Description", "InstalledOn","Caption" } $paramHash2 = @{ Head = $head2 Title = "Services Not Running Report" As = "Table" PostContent = "<H6>$(Get-Date)</H6>" Property = "ComputerName","Caption", "State" } $paramHash3 = @{ Head = $head3 Title = "Server Reboot Report" As = "Table" PostContent = "<H6>$(Get-Date)</H6>" Property = "ComputerName","Message", "TimeCreated" } $paramHash4 = @{ Head = $head4 Title = "Failed Windows Updates Report" As = "Table" PostContent = "<H6>$(Get-Date)</H6>" Property = "MachineName","Message", "TimeWritten" } $Data2 = @() $SRVReboot2 = @() $ServiceCheck2 = @() $Report = Foreach ($a in $pc) { $SRVReboot = get-winevent -ComputerName $a -FilterHashtable @{Logname='system';ID=6013} -MaxEvents 1 | Select-Object machinename, Message,TimeCreated foreach ($srvr in $SRVReboot) { $SRVRebootItem = new-object PSObject $SRVRebootItem | add-member NoteProperty "Computername" $srvr.machinename $SRVRebootItem | add-member NoteProperty "Message" $srvr.Message $SRVRebootItem | add-member NoteProperty "TimeCreated" $srvr.TimeCreated $SRVReboot2 += $SRVRebootItem } $data = Get-HotFix -ComputerName $a | ? installedon -gt $pdate | Select-Object PSComputerName, Caption, Description, hotfixid, installedon, name foreach ($d in $data) { $dataItem = new-object PSObject $dataItem | add-member NoteProperty "ComputerName" $d.PSComputerName $dataItem | add-member NoteProperty "Caption" $d.Caption $dataItem | add-member NoteProperty "Description" $d.Description $dataItem | add-member NoteProperty "hotfixid" $d.hotfixid $dataItem | add-member NoteProperty "installedon" $d.installedon $dataItem | add-member NoteProperty "name" $d.name $Data2 += $dataItem } $ServiceCheck = Get-WmiObject Win32_Service -ComputerName $a |Where-Object { $_.StartMode -eq 'Auto' -and $_.State -ne 'Running' } |Select-Object PSComputerName, Caption, Name, State foreach ($sc in $ServiceCheck) { $ServiceCheckItem = new-object PSObject $ServiceCheckItem | add-member NoteProperty "ComputerName" $sc.PSComputerName $ServiceCheckItem | add-member NoteProperty "Caption" $sc.Caption $ServiceCheckItem | add-member NoteProperty "Name" $sc.Name $ServiceCheckItem | add-member NoteProperty "State" $sc.State $ServiceCheck2 += $ServiceCheckItem } } $SRVReboot2 = $SRVReboot2 | ConvertTo-Html @paramHash3 $ServiceCheck2 = $ServiceCheck2 | ConvertTo-Html @paramHash2 $Data2 = $Data2 | ConvertTo-Html @paramHash1 $Report = $data2 + $ServiceCheck2 + $SRVReboot2 Send-MailMessage @smtpsettings -Body "$Report" -BodyAsHtml
- Marked as answer by Jeff_at_ZF Tuesday, January 16, 2018 1:34 PM
Tuesday, January 16, 2018 1:33 PM