以下は稼働時間をチェックします。私が考えていなかった条件の 1 つ (今のところを除く) は、サーバーに ping を実行できるが、アップタイムを取得できない場合です。その場合、エラーを発生させるには以下のスクリプトが必要です。これを行う方法が思いつきません - アイデアはありますか?
コード:
#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"
}
}