0

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"

1 Answers1

0

Try this

$toreboot = @()
$rebooted = @()
[timespan]$recentboot = new-timespan $(get-date).AddHours(-10) $(get-date)
[timespan]$needboot = new-timespan $(get-date) $(get-date).Adddays(30) 

$recentboot
$needboot

$names = Get-Content "d:\scrap\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"
                if ($uptime -lt $recentboot)
                    {$rebooted += $names}
                if($uptime -gt $needboot)
                    {$toreboot += $name}

               }
     else {
        Write-output "$name is not pinging"
          }

          if ($toreboot -ne $null)
            {set-content -Path "d:\scrap\serverstoboot.txt" -Value $toreboot}
          if ($rebooted -ne $null)
            {set-content -Path "d:\scrap\recentlybooted.txt" -value $rebooted}
    }