【问题标题】:PowerShell get applied windows updates HTML reportPowerShell 获取已应用的 Windows 更新 HTML 报告
【发布时间】:2013-11-17 15:01:40
【问题描述】:

我有一个 PowerShell 脚本,它从 2003 服务器获取应用的 Windows 更新并将结果输出到 .HTML 报告。我喜欢帮助的是如何让每个修补程序在单独的行上列出,而不是像下面的示例中那样自动换行?

下面是脚本

$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

【问题讨论】:

    标签: html windows powershell report updates


    【解决方案1】:

    我添加了一个遍历每个更新事件的 foreach 循环,并删除了看起来多余的 newobj 代码。这行得通吗?

    $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
    

    【讨论】:

      【解决方案2】:

      查看使用-Fragment-As LIST 参数的ConvertTo-Html cmdlet。

      如果消息在一个数组中,它将把它转换成你想要的 HTML

      【讨论】:

      • 谢谢,但这是我需要更改的一件事的示例,而不是完整的脚本。格式需要以我为例。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多