我有一些 PowerShell 代码可以获取服务器列表的正常运行时间,并输出服务器正常运行的天数、小时数和分钟数。
我正在尝试添加一条运行时间少于 10 小时并输出到文本文件的语句 - 重新启动
如果服务器运行 30 天或更长时间,则文本的输出将是 - Reboot Needed
我只是不确定如何做到这一点。这是我的正常运行时间...
$names = Get-Content "C:\Users\david.sechler\Documents\PowerShell\Get Uptime\servers.txt"
@(
foreach ($name in $names)
{
if ( Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue )
{
$wmi = gwmi -class Win32_OperatingSystem -computer $name
$LBTime = $wmi.ConvertToDateTime($wmi.Lastbootuptime)
[TimeSpan]$uptime = New-TimeSpan $LBTime $(get-date)
Write-output "$name Uptime is $($uptime.days) Days $($uptime.hours) Hours $($uptime.minutes) Minutes $($uptime.seconds) Seconds"
}
else {
Write-output "$name is not pinging"
}
}
) | Out-file -FilePath "C:\Users\david.sechler\Documents\PowerShell\Get Uptime\results.txt"