2013-10-28 110 views
2

不想使用随APC一起提供的APC软件我已经编写了一个PowerShell脚本来监视我的UPS并在电池电量达到30%时启动我的服务器的关闭顺序。当PowerShell脚本失败时,我的选择是什么?

脚本通过USB重定向在虚拟化服务器上​​运行。该窗口始终处于打开状态,因为它监视主UPS的电池电量的WMI输入。该脚本工作很漂亮,但几次我登录到服务器,发现屏幕上出现以下错误

Get-WmiObject : Call was canceled by the message filter. (Exception from HRESULT: 0x80010002 (RPC_E_CALL_CANCELED)) 
At C:\Users\user\Desktop\upsmonitor.ps1:38 char:27 
+  $binfo = Get-WMIObject <<<< -class Win32_Battery -computername $system -namespace root\cimv2 
    + CategoryInfo   : InvalidOperation: (:) [Get-WmiObject], COMException 
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand 

我认为这是通过WMI活动造成的昙花一现 - 我读过论坛认为这是由WMI崩溃造成的。无论如何,这是有问题的,因为如果脚本需要运行,并且出错了,那么它对我没有好处。检查服务器经常是不理想的,所以我想办法要么

  • 失败后
  • 重新执行脚本在最低限度时通知我的脚本失败

我已经读入-ErrorAction交换机,但我不确定如何在我的情况下实施它,或者如果它甚至适用。通常情况下,我只是玩弄脚本,直到我能够解决问题,但它只是真的出现在我的生产环境中,并且不一致。解决此问题可能需要几个月,等待发生错误,希望我的修复工作。

我希望你们能提供任何帮助。

脚本如下所示。

## Variable Declaration 
cls 
$system = "." 
$namespace = "root\CIMV2" 

#SMTP Variables (for e-mail notifications) 
$emailFrom = "[email protected]" 
$emailto = "[email protected]" 
$subject = "UPS Notifications" 
$PSEmailServer = "10.0.0.100" 

#Check to see if the event log source exists, and create it if it doesn't 
$sourceExists = get-eventlog -list | where-object {$_.logdisplayname -eq "UPS Monitor"} 
if (! $sourceExists) { 
    new-eventlog -LogName "UPS Monitor" -source "UPS Monitor" 
    } 

#Send the administrator a message to inform them of monitoring 
$initialStatus = Get-WMIObject -class Win32_Battery -computer $system -namespace $namespace 
send-mailmessage -From $emailFrom -To $emailTo -Subject $subject -Body "UPS Monitor Online: Monitoring UPS Status." 
echo "UPS Monitor Online - Currently Monitoring UPS Status. DO NOT CLOSE THIS WINDOW!" 

$eruntime = $initialstatus.estimatedruntime 

#What's the status of the Battery upon starting the script 
if ($initialStatus.batterystatus -eq 2) { 
    echo "Battery Status : On AC Power" 
    echo "Estimated Time remaining on charge : $eruntime minutes" 
} elseif($initialStatus.batterystatus -eq 1) { 
    echo "Battery Status : Discharging" 
    echo "Estimated Time remaining on charge : $eruntime minutes" 
    } 

write-eventlog -logname "UPS Monitor" -Source "UPS Monitor" -EntryType Information -EventID 1 -Message "UPS Monitor Online: Currently Monitoring UPS Status" 

while($true) 
    { 
    $binfo = Get-WMIObject -class Win32_Battery -computername $system -namespace root\cimv2 
    if($binfo.BatteryStatus -eq 2) { 
     #On AC Power - No action required 
     } 
     if($binfo.BatteryStatus -eq 1){ 
      #If UPS status is 1, UPS is discharging - Notifications will begin at 80% Battery Level 
      #echo "Battery Charge Percentage : " $binfo.EstimatedChargeRemaining 
      if($binfo.EstimatedChargeRemaining -eq 80){ 
       #When the battery level gets to 80% write an event to the event log and e-mail the administrator 
       write-eventlog -logname "UPS Monitor" -Source "UPS Monitor" -EntryType Information -EventID 1 -Message "Power Failure Detected - 80% Battery Life Remaining" 
       send-mailmessage -From $emailFrom -To $emailTo -Subject $subject -Body "Power Failure Detected; Servers on Battery Power. Battery level is 80%" 
       start-sleep -seconds 30 
       } 
      elseif($binfo.EstimatedChargeRemaining -eq 60){ 
       #When the battery level gets to 60% write an event to the event log and e-mail the administrator 
       write-eventlog -logname "UPS Monitor" -Source "UPS Monitor" -EntryType Information -EventID 1 -Message "Power Failure Detected : 60% Battery Life Remaining" 
       send-mailmessage -From $emailFrom -To $emailTo -Subject $subject -Body "Power Failure Detected; Servers on Battery Power. Battery level is 60%" 
       start-sleep -seconds 30 
       } 
      elseif($binfo.EstimatedChargeRemaining -eq 40){ 
       #When the battery level gets to 40% write an event to the event log and e-mail the administrator and warn of shutdown 
       write-eventlog -logname "UPS Monitor" -Source "UPS Monitor" -EntryType Information -EventID 1 -Message "Power Failure Detected : 40% Battery Life Remaining" 
       send-mailmessage -From $emailFrom -To $emailTo -Subject $subject -Body "Power Failure Detected & Reserve battery is critical. Servers will be restarted at 30% battery life if AC power is not resumed" 
       start-sleep -seconds 30 
       } 
       elseif($binfo.EstimatedChargeRemaining -le 30){ 
       #When the battery level gets to 30% being shutting down servers 
       write-eventlog -logname "UPS Monitor" -Source "UPS Monitor" -EntryType Information -EventID 1 -Message "Critical Battery Threshold Reached : Commencing Shutdown of servers" 
       send-mailmessage -From $emailFrom -To $emailTo -Subject $subject -Body "Power Failure Detected & Critical Battery Threshold has been Reached. Commencing Shutdown of Servers" 
       start-sleep -seconds 15 
       stop-computer -cn (Get-Content C:\Users\User\Desktop\serverlist.txt) -Force 
       } 

     } 
    } 

回答

0

您可以简单地使用try catch块并在发生错误时在catch块中定义特定的细节。

相关问题