2013-05-31 37 views
0

以下檢查正常運行時間。我沒有叮叮的一種情況(現在除外)是,如果服務器可以ping通,但無法獲得正常運行時間 - 如果出現這種情況,我需要下面的腳本來出錯。我想不出如何做到這一點 - 任何想法?檢查ping和正常運行時間?

CODE:

#clear 
$Computers = Get-Content "E:\DATA\PS_Jobs\Patching_Uptime\Saturday_Servers.txt" 

Foreach($computer in $Computers) 
{ 

    if (Test-Connection -ComputerName $computer -Quiet) 
    { 

    $LastBoot = (Get-WmiObject -Class Win32_OperatingSystem -computername $computer).LastBootUpTime 
    $sysuptime = (Get-Date) – [System.Management.ManagementDateTimeconverter]::ToDateTime($LastBoot) 

    $days = $sysuptime.Days 
    $DaystoHours = ($sysuptime.Days)*24 
    $hours = $sysuptime.hours 
    $TotalHours = $DaystoHours + $hours 

     if($TotalHours -gt '12') 
     { 
      Write-EventLog -LogName WinLondonUptime -Source Uptime -EntryType Error -EventId 5 -Message "$computer - FAILED - Servers Uptime is GREATER then 12 hours or not contactable - Uptime is $days Days and $hours Hours - This is the Saturday patching run" 
     } 
     else 
     { 
      Write-EventLog -LogName WinLondonUptime -Source Uptime -EntryType Information -EventId 4 -Message "$computer - SUCCESS - Servers uptime is less than 12 hours - Uptime is $days Days and $hours Hours - This is the Saturday patching run" 
     } 
    } 
    else 
     { 
      Write-EventLog -LogName WinLondonUptime -Source Uptime -EntryType Error -EventId 5 -Message "$computer - FAILED - Server is not contactable - This is the Saturday patching run" 
     } 
} 
+0

雖然我不確定這是否是偏離主題,但我認爲您有更好的機會在[su]上獲得PowerShell的答案。 –

+0

您可以計算運行時間越來越最長的運行過程或查詢系統事件日誌獲取事件ID 6005 –

+0

覺得我問這個稍微不正確 - 很多你在想我想的正常運行時間 - 這是不正確 - 上面的代碼確實得到了正常運行時間。我特別詢問何時UPTIME不可用於我希望它出錯的特定服務器....即使服務器已啓動並執行ping操作。 – lara400

回答

1

您可以在一個try/catch封裝你的代碼,並使用一個時間跨度對象。像這樣的東西(未測試,但它給你的想法):

try 
    { 
     Test-Connection -ComputerName $computer -Count 1 -ErrorAction Stop 

     $wmi = Get-WmiObject Win32_OperatingSystem -computer $computer 
     $time = $wmi.ConvertToDateTime($wmi.Lastbootuptime) 
     [TimeSpan] $uptime = New-TimeSpan $time $(get-date) 

     if ($uptime.Hours -gt 12) 
     { 
      Write-EventLog -LogName WinLondonUptime -Source Uptime -EntryType Error -EventId 5 -Message "$computer - FAILED - Servers Uptime is GREATER then 12 hours or not contactable - Uptime is $days Days and $hours Hours - This is the Saturday patching run" 
     } 
     else 
     { 
      Write-EventLog -LogName WinLondonUptime -Source Uptime -EntryType Information -EventId 4 -Message "$computer - SUCCESS - Servers uptime is less than 12 hours - Uptime is $($uptime.Days) Days and $($uptime.Hours) Hours - This is the Saturday patching run" 
     } 
    } 

    catch [System.Management.Automation.ActionPreferenceStopException] 
    { 
     Write-EventLog -LogName WinLondonUptime -Source Uptime -EntryType Error -EventId 5 -Message "$computer - FAILED - Server is not contactable - This is the Saturday patching run" 
    } 
+0

感謝大衛 - 這幫助了我,並認爲你知道我在問什麼! – lara400

1

你可以得到使用WMI還得到一臺電腦的最後引導時:

$wmi = [WMI] "" 
$operatingSystem = get-wmiobject Win32_OperatingSystem -computername "." 
$wmi.ConvertToDateTime($operatingSystem.LastBootUpTime) 

比爾

+0

謝謝你,但我想我有點不正確 - 很多人以爲我想要得到正時 - 這是不正確的 - 上面的代碼確實得到正常運行時間。我特別詢問何時UPTIME不可用於我希望它出錯的特定服務器....即使服務器已啓動並執行ping操作。 – lara400

0

示數出通常被定義爲返回一個非零退出代碼,這可以通過

完成

Exit -1 

或者任何號碼,你更喜歡。

你也可以拋出異常,以及,如果你要發送一個特定的錯誤消息和一個非常討厭的錯誤,有一個答案here

1

此人那種與WMI not having a timeout feature同樣的問題。你可能會嘗試創建一個工作來查詢WMI並獲取LBUT。然後在工作中設置一個計時器。但是如果你只想做一些錯誤處理,你應該使用try{}catch{}塊。

try{ 
    $lbut = (Get-WmiObject -Class Win32_OperatingSystem -computername $computer).LastBootUpTime 
} 
catch{ 
    "FAILED" 
    continue 
} 

You might want to look at using CIM instead of WMI

+0

我會試試這個 - 看起來類似於David用try catch方法給出的。 – lara400

0

我不能對此進行測試從這裏開始的時刻,但也許嘗試if語句嵌套另一個枚舉調用WMI是成功還是失敗?

if (Test-Connection -ComputerName $computer -Quiet) 
    { 

    if((Get-WmiObject -Class Win32_OperatingSystem -computername $computer).LastBootUpTime){ 

     $LastBoot = (Get-WmiObject -Class Win32_OperatingSystem -computername $computer).LastBootUpTime 
     $sysuptime = (Get-Date) – [System.Management.ManagementDateTimeconverter]::ToDateTime($LastBoot) 
0

我的解決方案如下。我在ping主機名並獲取啓動時間,如果它沒有得到錯誤。

[System.Net.Dns]::GetHostAddresses("hostname").ipaddresstostring 
if($? -eq $true){ 
    Get-CimInstance -ClassName win32_operatingsystem | select lastbootuptime 
}