I have some PowerShell code that gets me the uptime of a list of servers and outputs the Days, Hours and Minutes that the server is up.
I'm trying to add a statement that will take anything less than 10 hours of uptime and output to the text file - Rebooted
If the server is up for 30 days or more, the output to text would be - Reboot Needed
I'm just not sure how to make that happen. Here is what I have for uptime...
$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"