├── apccontrol.bat ├── README.md └── apcupsd.ps1 /apccontrol.bat: -------------------------------------------------------------------------------- 1 | Powershell.exe -ExecutionPolicy Bypass -File C:\apcupsd\etc\apcupsd\apcupsd.ps1 %1 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # APCUPSD Notifier 2 | Powershell script to notify on APCUPSD events and shut down computer. 3 | 4 | Uses Twilio SMS and System.Net.Mail.Mailmessage for delivering notifications. 5 | 6 | Copy apccontrol.bat and apcupsd.ps1 to C:\apcupsd\etc\apcupsd (or your apcupsd script dir) then change the variables in apcupsd.ps1. 7 | 8 | Note: review path in apccontrol.bat. 9 | -------------------------------------------------------------------------------- /apcupsd.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | +-+-+-+-+-+-+-+-+-+ 3 | | A P C U P S D | 4 | +-+-+-+-+-+-+-+-+-+ 5 | 6 | .SYNOPSIS 7 | APCUPSD Powershell Event Scripts 8 | 9 | .DESCRIPTION 10 | Common Code for APCUPSD Powershell Event Scripts 11 | 12 | .FUNCTIONALITY 13 | Called from apccontrol.bat. 14 | 15 | .PARAMETER 16 | 17 | 18 | .NOTES 19 | 20 | 21 | .EXAMPLE 22 | 23 | 24 | #> 25 | 26 | Param([string]$Event) 27 | $Event = $Event.Trim() 28 | 29 | <### TWILIO VARIABLES ###> 30 | $SID = "AC..............................2c" 31 | $Token = "27............................23" 32 | $SMSFrom = "+12125551212" 33 | 34 | <### EMAIL VARIABLES ###> 35 | $FromAddress = 'notify@mydomain.tld' 36 | $Subject = 'APCUPSD Power Event Notification' 37 | $SMTPServer = 'mydomain.tld' 38 | $SMTPAuthUser = 'notify@mydomain.tld' 39 | $SMTPAuthPass = 'supersecretpassword' 40 | $SMTPPort = 587 41 | $SSL = $True 42 | $UseHTML = $False 43 | $Recipients = @{ 44 | "admin@mydomain.tld" = "+17185551718" 45 | "user@gmail.com" = "+16465551646" 46 | } 47 | 48 | <### FUNCTIONS ###> 49 | 50 | Function Debug ($DebugOutput) { 51 | Write-Output "$(Get-Date -f G) : $DebugOutput" | Out-File "$PSScriptRoot/apcupsd-debug.log" -Encoding ASCII -Append 52 | } 53 | 54 | # Function to send SMS notification via Twilio 55 | Function SendSMS ($Num, $Msg) { 56 | # Try to send message 57 | Try { 58 | $URL = "https://api.twilio.com/2010-04-01/Accounts/" + $SID + "/Messages.json" 59 | $Params = @{ To = $Num; From = $SMSFrom; Body = $Msg } 60 | $TokenSecureString = $Token | ConvertTo-SecureString -asPlainText -Force 61 | $Credential = New-Object System.Management.Automation.PSCredential($SID, $TokenSecureString) 62 | $Request = Invoke-WebRequest $URL -Method Post -Credential $Credential -Body $Params -UseBasicParsing 63 | $SentMsg = $Request | ConvertFrom-Json 64 | $StatusCode = $Request | Select-Object -Expand StatusCode 65 | } 66 | 67 | # If error, then throw error that script will pick up 68 | Catch { 69 | Debug "ERROR : Twilio Send : $($Error[0])" 70 | Throw $Error[0] 71 | } 72 | 73 | # If status code indicates message not sent, then throw error that script will pick up 74 | If ($StatusCode -ne 201) { 75 | Debug "ERROR : Twilio Send : StatusCode: $StatusCode" 76 | Throw $StatusCode 77 | } 78 | } 79 | 80 | # Function to send email notification 81 | Function SendEmail ($Body, $Recipient) { 82 | Try { 83 | $Message = New-Object System.Net.Mail.Mailmessage $FromAddress, $Recipient, $Subject, $Body 84 | $Message.IsBodyHTML = $UseHTML 85 | $SMTP = New-Object System.Net.Mail.SMTPClient $SMTPServer,$SMTPPort 86 | $SMTP.EnableSsl = $SSL 87 | $SMTP.Credentials = New-Object System.Net.NetworkCredential($SMTPAuthUser, $SMTPAuthPass); 88 | $SMTP.Send($Message) 89 | } 90 | Catch { 91 | Debug "Email ERROR : $($Error[0])" 92 | } 93 | } 94 | 95 | # Function to return event notification message 96 | Function EventMessage ($Event) { 97 | Switch ($Event) { 98 | "commfailure" {Return "UPS communication error"; Break} 99 | "commok" {Return "UPS communication restored"; Break} 100 | "powerout" {Return "The power is out!"; Break} 101 | "onbattery" {Return "Network on battery power"; Break} 102 | "offbattery" {Return "Network power restored"; Break} 103 | "mainsback" {Return "The power is restored"; Break} 104 | "failing" {Return "UPS battery power exhausted - doing shutdown"; Break} 105 | "timeout" {Return "UPS battery runtime limit exceeded - doing shutdown"; Break} 106 | "loadlimit" {Return "UPS battery discharge limit reached - doing shutdown"; Break} 107 | "runlimit" {Return "UPS battery runtime percent reached - doing shutdown"; Break} 108 | "doshutdown" {Return "Initiating server shutdown due to UPS runtime exceeded"; Break} 109 | "annoyme" {Return "UPS power problems - please logoff"; Break} 110 | "emergency" {Return "Emergency UPS shutdown initiated"; Break} 111 | "changeme" {Return "Emergency! UPS batteries have failed: Change them NOW"; Break} 112 | "remotedown" {Return "Shutdown due to master state or comms lost"; Break} 113 | "startselftest" {Return "UPS self-test starting"; Break} 114 | "endselftest" {Return "UPS self-test completed"; Break} 115 | "battdetach" {Return "UPS battery disconnected"; Break} 116 | "battattach" {Return "UPS battery reattached"; Break} 117 | Default { 118 | Debug "Unsupported Event : $Event : Quitting Script" 119 | Exit 120 | } 121 | } 122 | } 123 | 124 | <### START SCRIPT ###> 125 | 126 | # Create message 127 | $Msg = EventMessage $Event 128 | 129 | # Send to recipients 130 | ForEach ($Key in $Recipients.Keys) { 131 | Try {SendSMS $($Recipients[$Key]) $Msg} 132 | Catch {SendEmail $($Key) $Msg} 133 | } 134 | 135 | # Send messagage to debug log 136 | Debug $Msg 137 | 138 | # If doshutdown command, then shut down the computer 139 | If ($Event -eq "doshutdown") { 140 | Start-Sleep -seconds 7 141 | & shutdown /s /f /t 7 142 | Exit 99 143 | } 144 | --------------------------------------------------------------------------------