Добавление оператора if для изменения вывода текста

У меня есть некоторый код PowerShell, который дает мне время безотказной работы списка серверов и выводит дни, часы и минуты, в течение которых сервер работает.

Я пытаюсь добавить заявление, которое займет менее 10 часов безотказной работы и вывода в текстовый файл - Перезагрузка

Если сервер работает 30 дней или более, вывод в тексте будет «Требуется перезагрузка».

Я просто не знаю, как это сделать. Вот что у меня есть для безотказной работы...

    $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"

person David Sechler    schedule 23.04.2015    source источник
comment
Какой это язык? пакетный файл?   -  person Degustaf    schedule 24.04.2015
comment
Извини Дэн, хороший вопрос! Это пауэршелл.   -  person David Sechler    schedule 24.04.2015


Ответы (1)


Попробуй это

$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}
    }
person Douglas Tripple    schedule 04.06.2015