【问题标题】:Reboot check is showing all reboots not just last重新启动检查显示所有重新启动,而不仅仅是最后一次
【发布时间】:2017-11-29 19:16:48
【问题描述】:

我有一个在 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
}

【问题讨论】:

  • 我已尽我所能重新格式化以提高可读性。

标签: powershell reboot


【解决方案1】:

您只需要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 调用没有任何要转换的内容。他们将创建一个带有空表的空 HTML 文档,并将其附加到您的文件中。

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

【讨论】:

  • 谢谢培根,我稍微修改了一下,效果很好,你赢得了绿色支票
猜你喜欢
  • 1970-01-01
  • 2020-01-21
  • 1970-01-01
  • 2021-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-27
  • 2017-06-04
相关资源
最近更新 更多