0

下面检查正常运行时间。我没有想到的一个条件(现在除外)是服务器是否可以被 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"
        }
}
4

6 回答 6

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"
    }
于 2013-05-31T14:58:06.917 回答
1

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

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

账单

于 2013-05-31T14:11:54.787 回答
1

这个人有同样的问题,WMI 没有超时功能。您可能会尝试创建一个作业来查询 WMI 并获取 LBUT。然后只需在工作上设置一个计时器。但是如果你只想做一些错误处理,你应该使用try{}andcatch{}块。

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

您可能想看看使用 CIM 而不是 WMI

于 2013-05-31T15:03:44.077 回答
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)
于 2013-06-01T12:26:07.713 回答
0

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

Exit 1

或者

Exit -1

或者你喜欢的任何数字。

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

于 2013-05-31T14:13:36.783 回答
0

我的解决方案如下。如果没有出现错误,我正在 ping 主机名并获取启动时间。

[System.Net.Dns]::GetHostAddresses("hostname").ipaddresstostring
if($? -eq $true){
   Get-CimInstance -ClassName win32_operatingsystem | select lastbootuptime
}
于 2017-05-24T08:40:56.837 回答