重新启动检查显示所有重新启动,而不仅仅是最后一次

Reboot check is showing all reboots not just last

我有一个 运行 post MW 的重启检查脚本,我需要它只提取最后一次重启以验证服务器是否已重启,目前它们提取所有重启历史记录。下面是我的脚本:

$DHCP = (Get-Content -Path "\termserv\d$\SERVER1\SERVER2\Scripts\morescripts\DHCPServers.txt")

foreach ($Server in $DHCP) {
    Get-Service -ComputerName $Server -DisplayName "DHCP Server" |
        ConvertTo-Html -Title "PScomputername" -Body "<H3> SERVER2 Uptime Report </H3> " -Property PSComputername >> \termserv\d$\SERVER1\SERVER2\Server3\SERVER2.html
    Get-Service -ComputerName $Server -DisplayName "DHCP Server" |
        ConvertTo-Html -Property MachineName,Status,ServiceName | 
        foreach {
            if ($_ -like "*<td>Running</td>*") {
                $_ -replace "<tr>", "<tr bgcolor=green>"
            } elseif ($_ -like "*<td>Stopped</td>*") {
                $_ -replace "<tr>", "<tr bgcolor=red>"
            } else {
                $_
            }
        } >> \termserv\d$\SERVER1\SERVER2\Server3\SERVER2.html
    Get-WmiObject win32_operatingsystem -ComputerName $Server | 
        Select PSComputername, @{n='BootTime';e={$_.ConvertToDateTime($_.LastBootupTime)}} |
        ConvertTo-Html -Property PSComputerName,BootTime >> \termserv\d$\SERVER1\SERVER2\Server3\SERVER2.html
    ConvertTo-Html -Property PSComputerName,Installedon,Description,caption >> \termserv\d$\SERVER1\SERVER2\Server3\SERVER2.html
}


$Print = (Get-Content -Path "\termserv\d$\SERVER1\SERVER2\Scripts\morescripts\PrintServers.txt")

foreach ($Server in $Print) {
    Get-Service -ComputerName $Server -DisplayName "Print Spooler" |
        ConvertTo-Html -Property MachineName,Status,ServiceName | 
        foreach {
            if ($_ -like "*<td>Running</td>*") {
                $_ -replace "<tr>", "<tr bgcolor=green>"
            } elseif ($_ -like "*<td>Stopped</td>*") {
                $_ -replace "<tr>", "<tr bgcolor=red>"
            } else {
                $_
            }
        } >> \termserv\d$\SERVER1\SERVER2\Server3\SERVER2.html
    Get-WmiObject win32_operatingsystem -ComputerName $Server | 
        Select PSComputername, @{n='BootTime';e={$_.ConvertToDateTime($_.LastBootupTime)}} | 
        ConvertTo-Html -Property PSComputerName,BootTime >> \termserv\d$\SERVER1\SERVER2\Server3\SERVER2.html
    ConvertTo-Html -Property PSComputerName,Installedon,Description,caption >> \termserv\d$\SERVER1\SERVER2\Server3\SERVER2.html
}

您只需要 Sort-Object 计算 属性 然后告诉 Select-Object 选择第一项:

Get-WmiObject -ClassName Win32_OperatingSystem -ComputerName $Server |
    Sort-Object -Property @{e={$_.ConvertToDateTime($_.LastBootupTime)}} -Descending |
    Select-Object -First 1 -Property [...] |
    ConvertTo-Html [...]

应该注意的是,据我所知,每个主 foreach 循环中的最终 ConvertTo-Html 调用没有任何要转换的内容。他们将创建一个带有空 table 的空 HTML 文档,并将其附加到您的文件中。

此外,您要将多个 HTML 文档附加到 server2.html 文件。如果它们将在同一个文档中,您应该以 <html><head><title /></head><body> 开始整个文件,然后在所有 ConvertTo-Html 调用中使用 -Fragment 参数,最后关闭文档</body></html>。您当前的方法可能有效,但您正在生成一个明确无效的 HTML 文档。正确呈现的浏览器应该只显示第一个 table.