检查ping然后正常运行时间?

时间:2013-05-31 13:59:16

标签: powershell powershell-v2.0 powershell-v3.0

以下检查正常运行时间。我没有讨论的一个条件(现在除外)是服务器是否可以ping通但是无法获取UPTIME - 如果是这种情况,我需要以下脚本错误输出。我想不出怎么做 - 任何想法?

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

6 个答案:

答案 0 :(得分:1)

您还可以使用WMI获取计算机的上次启动时间:

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

比尔

答案 1 :(得分:1)

您可以将代码封装在try / catch中并使用timepan对象。像这样的东西(没有测试,但它给你的想法):

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

答案 2 :(得分: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

答案 3 :(得分:0)

错误输出通常被定义为返回非零退出代码,这可以通过

完成
Exit 1

Exit -1

或者你喜欢的任何数字。

你也可以抛出异常,如果你想发送一个特定的错误信息和一个非常讨厌的错误,那就有答案here

答案 4 :(得分: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)

答案 5 :(得分:0)

我的解决方案如下。我正在ping主机名并获得启动时间,如果它没有收到错误。

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