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