2015-04-23 5 views
0

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

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

Если сервер работает в течение 30 дней или более, выход в текст будет be - 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" 
+0

Какой язык это? [Тег: пакетный файл]? – Degustaf

+0

Извините Дэн, хороший вопрос! Это powershell. –

ответ

0

Попробуйте

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