Guest User

Untitled

a guest
Mar 17th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.11 KB | None | 0 0
  1. @echo off
  2. setlocal
  3.  
  4. rem
  5. rem This is the Windows apccontrol file.
  6. rem
  7.  
  8. rem Assign parameters to named variables
  9. SET command=%1
  10. SET sbindir=%5
  11.  
  12. rem Strip leading and trailing quotation marks from paths.
  13. rem This is easily accomplished on NT, but Win95/98/ME
  14. rem require an evil little trick with 'FOR'.
  15. SET sbindir=%sbindir:"=%
  16. IF "%sbindir%" == "" FOR %%A IN (%5) DO SET sbindir=%%A
  17.  
  18. rem Paths to important executables
  19. SET APCUPSD="%sbindir%\apcupsd"
  20. SET SHUTDOWN="%sbindir%\shutdown"
  21. SET BACKGROUND="%sbindir%\background"
  22.  
  23. rem Only do popups on Win95/98/ME/NT. All other platforms support
  24. rem balloon notifications which are provided by apctray.
  25. SET POPUP=echo
  26. VER | FIND /I "Windows 95" > NUL
  27. IF NOT ERRORLEVEL 1 SET POPUP=%BACKGROUND% "%sbindir%\popup"
  28. VER | FIND /I "Windows 98" > NUL
  29. IF NOT ERRORLEVEL 1 SET POPUP=%BACKGROUND% "%sbindir%\popup"
  30. VER | FIND /I "Windows ME" > NUL
  31. IF NOT ERRORLEVEL 1 SET POPUP=%BACKGROUND% "%sbindir%\popup"
  32. VER | FIND /I "Windows NT" > NUL
  33. IF NOT ERRORLEVEL 1 SET POPUP=%BACKGROUND% "%sbindir%\popup"
  34.  
  35. rem
  36. rem This piece is to substitute the default behaviour with your own script,
  37. rem perl, C program, etc.
  38. rem
  39. rem You can customize any command by creating an executable file (may be a
  40. rem script or a compiled program) and naming it the same as the %1 parameter
  41. rem passed by apcupsd to this script. We will accept files with any extension
  42. rem included in PATHEXT (*.exe, *.bat, *.cmd, etc).
  43. rem
  44. rem After executing your script, apccontrol continues with the default action.
  45. rem If you do not want apccontrol to continue, exit your script with exit
  46. rem code 99. E.g. "exit /b 99".
  47. rem
  48. rem WARNING: please be aware that if you add any commands before the shutdown
  49. rem in the downshutdown) case and your command errors or stalls, it will
  50. rem prevent your machine from being shutdown, so test, test, test to
  51. rem make sure it works correctly.
  52. rem
  53. rem The apccontrol.bat file will be replaced every time apcupsd is installed,
  54. rem so do NOT make event modifications in this file. Instead, override the
  55. rem event actions using event scripts as described above.
  56. rem
  57.  
  58. rem Use CALL here because event script might be a batch file itself
  59. CALL ".\%command%" 2> NUL
  60.  
  61. rem This is retarded. "IF ERRORLEVEL 99" means greater-than-or-
  62. rem equal-to 99, so we have to synthesize an == using two IFs.
  63. rem Ahh, the glory of Windows batch programming. At least they
  64. rem gave us a NOT op.
  65. IF NOT ERRORLEVEL 99 GOTO :events
  66. IF NOT ERRORLEVEL 100 GOTO :done
  67.  
  68. :events
  69.  
  70. rem
  71. rem powerout, onbattery, offbattery, mainsback events occur
  72. rem in that order.
  73. rem
  74.  
  75. IF "%command%" == "commfailure" GOTO :commfailure
  76. IF "%command%" == "commok" GOTO :commok
  77. IF "%command%" == "powerout" GOTO :powerout
  78. IF "%command%" == "onbattery" GOTO :onbattery
  79. IF "%command%" == "offbattery" GOTO :offbattery
  80. IF "%command%" == "mainsback" GOTO :mainsback
  81. IF "%command%" == "failing" GOTO :failing
  82. IF "%command%" == "timeout" GOTO :timeout
  83. IF "%command%" == "loadlimit" GOTO :loadlimit
  84. IF "%command%" == "runlimit" GOTO :runlimit
  85. IF "%command%" == "doshutdown" GOTO :doshutdown
  86. IF "%command%" == "annoyme" GOTO :annoyme
  87. IF "%command%" == "emergency" GOTO :emergency
  88. IF "%command%" == "changeme" GOTO :changeme
  89. IF "%command%" == "remotedown" GOTO :remotedown
  90. IF "%command%" == "startselftest" GOTO :startselftest
  91. IF "%command%" == "endselftest" GOTO :endselftest
  92. IF "%command%" == "battdetach" GOTO :battdetach
  93. IF "%command%" == "battattach" GOTO :battattach
  94.  
  95. echo Unknown command '%command%'
  96. echo.
  97. echo Usage: %0 command
  98. echo.
  99. echo Warning: this script is intended to be launched by
  100. echo apcupsd and should never be launched by users.
  101. GOTO :done
  102.  
  103. :commfailure
  104. %POPUP% "Communications with UPS lost."
  105. powershell.exe -file send_email.ps1 -Body "Communications with UPS lost."
  106. GOTO :done
  107.  
  108. :commok
  109. %POPUP% "Communciations with UPS restored."
  110. powershell.exe -file send_email.ps1 -Body "Communciations with UPS restored."
  111. GOTO :done
  112.  
  113. :powerout
  114. GOTO :done
  115.  
  116. :onbattery
  117. %POPUP% "Power failure. Running on UPS batteries."
  118. powershell.exe -file send_email.ps1 -Body "Power failure. Running on UPS batteries."
  119. GOTO :done
  120.  
  121. :offbattery
  122. %POPUP% "Power has returned. No longer running on UPS batteries."
  123. powershell.exe -file send_email.ps1 -Body "Power has returned. No longer running on UPS batteries."
  124. GOTO :done
  125.  
  126. :mainsback
  127. GOTO :done
  128.  
  129. :failing
  130. %POPUP% "UPS battery power exhausted. Doing shutdown."
  131. powershell.exe -file send_email.ps1 -Body "UPS battery power exhausted. Doing shutdown."
  132. GOTO :done
  133.  
  134. :timeout
  135. %POPUP% "UPS battery runtime limit exceeded. Doing shutdown."
  136. powershell.exe -file send_email.ps1 -Body "UPS battery runtime limit exceeded. Doing shutdown."
  137. GOTO :done
  138.  
  139. :loadlimit
  140. %POPUP% "UPS battery discharge limit reached. Doing shutdown."
  141. powershell.exe -file send_email.ps1 -Body "UPS battery discharge limit reached. Doing shutdown."
  142. GOTO :done
  143.  
  144. :runlimit
  145. %POPUP% "UPS battery runtime percent reached. Doing shutdown."
  146. powershell.exe -file send_email.ps1 -Body "UPS battery runtime percent reached. Doing shutdown."
  147. GOTO :done
  148.  
  149. :doshutdown
  150. rem
  151. rem If you want to try to power down your UPS, uncomment
  152. rem out the following lines, but be warned that if the
  153. rem following shutdown -h now doesn't work, you may find
  154. rem the power being shut off to a running computer :-(
  155. rem Also note, we do this in the doshutdown case, because
  156. rem there is no way to get control when the machine is
  157. rem shutdown to call this script with --killpower. As
  158. rem a consequence, we do both killpower and shutdown
  159. rem here.
  160. rem Note that Win32 lacks a portable way to delay for a
  161. rem given time, so we use the trick of pinging a
  162. rem non-existent IP address with a given timeout.
  163. rem
  164. rem %APCUPSD% /kill
  165. rem ping -n 1 -w 5000 10.255.255.254 > NUL
  166. rem %POPUP% "Doing %APCUPSD% --killpower"
  167. rem %APCUPSD% --killpower
  168. rem ping -n 1 -w 12000 10.255.255.254 > NUL
  169. rem
  170. %SHUTDOWN% -h now
  171. GOTO :done
  172.  
  173. :annoyme
  174. %POPUP% "Power problems: please logoff."
  175. powershell.exe -file send_email.ps1 -Body "Power problems: please logoff."
  176. GOTO :done
  177.  
  178. :emergency
  179. %POPUP% "Emergency shutdown initiated."
  180. powershell.exe -file send_email.ps1 -Body "Emergency shutdown initiated."
  181. GOTO :done
  182.  
  183. :changeme
  184. %POPUP% "Emergency! UPS batteries have failed: Change them NOW"
  185. powershell.exe -file send_email.ps1 -Body "Emergency! UPS batteries have failed: Change them NOW"
  186. GOTO :done
  187.  
  188. :remotedown
  189. %POPUP% "Shutdown due to master state or comms lost."
  190. powershell.exe -file send_email.ps1 -Body "Shutdown due to master state or comms lost."
  191. GOTO :done
  192.  
  193. :startselftest
  194. %POPUP% "Self-test starting"
  195. powershell.exe -file send_email.ps1 -Body "Self-test starting"
  196. GOTO :done
  197.  
  198. :endselftest
  199. %POPUP% "Self-test completed"
  200. powershell.exe -file send_email.ps1 -Body "Self-test completed"
  201. GOTO :done
  202.  
  203. :battdetach
  204. %POPUP% "Battery disconnected"
  205. powershell.exe -file send_email.ps1 -Body "Battery disconnected"
  206. GOTO :done
  207.  
  208. :battattach
  209. %POPUP% "Battery reattached"
  210. powershell.exe -file send_email.ps1 -Body "Battery reattached"
  211. GOTO :done
  212.  
  213. :done
  214. rem That's all, folks
Add Comment
Please, Sign In to add comment