0

I have a PowerShell script that get's applied windows updates from 2003 servers and outputs the result to a .HTML report. What I like help on is how can I get each hotfix to list on a separate line instead of word-wrapped as in the below example? enter image description here

Below is the script

$servers = Get-Content 'c:\temp\servers.txt'

$total = $null
$html = @'
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
<title> Server Updates Report</title>
<STYLE TYPE="text/css">
<!--
td {
font-family: Tahoma;
font-size: 13px;
border-top: 1px solid #999999;
border-right: 1px solid #999999;
border-bottom: 1px solid #999999;
border-left: 1px solid #999999;
padding-top: 0px;
padding-right: 0px;
padding-bottom: 0px;
padding-left: 0px;
}
body {
margin-left: 5px;
margin-top: 5px;
margin-right: 0px;
margin-bottom: 10px;

table {
border: thin solid #000000;
}
-->
</style>
</head>
<body>
<table width='100%'>
<tr bgcolor='#CCCCCC'>
<td colspan='7' height='25' align='center'><strong><font color="#003399" size="4"  face="tahoma">Server Patch Report </font></strong></td>
</tr>
</table>
<table width='100%'><tbody>
<tr bgcolor=#CCCCCC>
<td width='20%' height='15' align='center'> <strong> <font color="#003399" size="2" face="tahoma" >Server Name</font></strong></td>
<td width='80%' height='15' align='center'> <strong> <font color="#003399" size="2" face="tahoma" >Patching Information</font></strong></td>
</tr>" 
</table>
<table width='100%'><tbody>
'@

foreach($server in $servers){
    $date = Get-Date '24/10/2013'
    $Updates = gwmi win32_ntlogevent -filter "logfile='system' and sourcename='ntservicepack'" -ComputerName $server | Where {$_.ConvertToDateTime($_.TimeWritten) -gt $date} 

    $newobj = new-object psobject
    $newobj | add-member -membertype noteproperty -name "Server" -value $server 
    $newobj | add-member -membertype noteproperty -name "Updates" -value $Updates.Message 
    $htmlserver = $newobj.Server
    $htmlupdates = $newobj.Updates

$current = " <tr bgcolor=#CCCCCC>
<td width='20%' align='center'>$htmlserver</td>
<td width='80%' align='left'>$htmlupdates</td>
</tr> 
"     
$total += $current 
} 

$HTMLEnd = @"
</div>
</body>
</html>
"@

$MainHtml= $html + $total + $HTMLEnd
$MainHtml  | Out-File "c:\temp\Report.html" -Append

2 Answers 2

1

I added a foreach-loop that goes through every update-event and removed the newobj code which seemed redundant. Does this work?

$servers = Get-Content 'c:\temp\servers.txt'

$total = $null
$html = @'
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
<title> Server Updates Report</title>
<STYLE TYPE="text/css">
<!--
td {
font-family: Tahoma;
font-size: 13px;
border-top: 1px solid #999999;
border-right: 1px solid #999999;
border-bottom: 1px solid #999999;
border-left: 1px solid #999999;
padding-top: 0px;
padding-right: 0px;
padding-bottom: 0px;
padding-left: 0px;
}
body {
margin-left: 5px;
margin-top: 5px;
margin-right: 0px;
margin-bottom: 10px;

table {
border: thin solid #000000;
}
-->
</style>
</head>
<body>
<table width='100%'>
<tr bgcolor='#CCCCCC'>
<td colspan='7' height='25' align='center'><strong><font color="#003399" size="4"  face="tahoma">Server Patch Report </font></strong></td>
</tr>
</table>
<table width='100%'><tbody>
<tr bgcolor=#CCCCCC>
<td width='20%' height='15' align='center'> <strong> <font color="#003399" size="2" face="tahoma" >Server Name</font></strong></td>
<td width='80%' height='15' align='center'> <strong> <font color="#003399" size="2" face="tahoma" >Patching Information</font></strong></td>
</tr>" 
</table>
<table width='100%'><tbody>
'@

foreach($server in $servers){
    $date = Get-Date '24/10/2013'
    gwmi win32_ntlogevent -filter "logfile='system' and sourcename='ntservicepack'" -ComputerName $server | Where {$_.ConvertToDateTime($_.TimeWritten) -gt $date} | ForEach-Object {

$current = " <tr bgcolor=#CCCCCC>
<td width='20%' align='center'>$server</td>
<td width='80%' align='left'>$($_.Message)</td>
</tr> 
"     
$total += $current 
} 
}
$HTMLEnd = @"
</div>
</body>
</html>
"@

$MainHtml= $html + $total + $HTMLEnd
$MainHtml  | Out-File "c:\temp\Report.html" -Append
Sign up to request clarification or add additional context in comments.

Comments

0

Take a look at using the ConvertTo-Html cmdlet using the -Fragment and -As LIST parameters.

If the messages are in an array, it will convert it to t HTML you want

1 Comment

Thanks but this is an example of one thing I need to change and not the full script. The format needs to be as my example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.