Advertisement
Guest User

Battery Check with Saturation

a guest
Jun 7th, 2022
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # ==============================================================================
  2. # == Created by Jeremy McMahan, McMahan TECH LLC. 2022-01-15 Rev. 1.0         ==
  3. # == Revision Notes:                                                          ==
  4. # ==   1.0: Initial Version.                                                  ==
  5. # ==============================================================================
  6. # Script Variables to pass:                                                    =
  7. #   deviceName: Platform variable {{asset_name}}                               =
  8. #   lastBatteryAlert: Platform {{asset_custom_field_last_battery_alert}}       =
  9. #   blockBatteryAlert: Platform {{asset_custom_field_block_battery_alert}}     =
  10. #   saturationDays: Integer limiting how often an alert will be raised.        =
  11. #   alertPercent: Integer setting allowable battery health % before alerting.  =
  12. #                                                                              =
  13. # Prerequisites:                                                               =
  14. #    A) You must have a custom asset field titled "Last Battery Alert"         =
  15. #       of type "date" to store last alert date for saturation limiting.       =
  16. #    B) You must have a custom asset field titled "Block Battery Alert"        =
  17. #       of type "check box" to store systems not to check or alert on.         =
  18. #       the output message to write to.                                        =
  19. #    C) To really leverage this script, create an automated remediation to     =
  20. #       trigger an email to the client offering to replace their battery or    =
  21. #       create a ticket etc.                                                   =
  22. #                                                                              =
  23. # ==============================================================================
  24. # ==============================================================================
  25.  
  26. Import-Module $env:SyncroModule
  27.  
  28. if (($lastBatteryAlert -eq $null) -or ($lastBatteryAlert -eq "")) { $lastBatteryAlert = (Get-Date).AddDays(-$saturationDays) }
  29. $alertBelow = [System.Double]$alertPercent
  30.  
  31. # Check if Battery Health Alerts are disabled and bypass script.
  32. if (!($blockBatteryAlert -eq "yes")) {
  33.     $compLast = $lastBatteryAlert
  34.     $compThis = Get-Date
  35.  
  36.     $tSpan = (New-TimeSpan -Start $compLast -End $compThis)
  37.  
  38.     # Check if the saturation threshold is reached and bypass remainder of script.
  39.     if ($tSpan.Days -ge $saturationDays) {
  40.  
  41.         # Do the check.
  42.         & powercfg /batteryreport /XML /OUTPUT "batteryreport.xml"
  43.         start-sleep 3
  44.        
  45.         if (![System.IO.File]::Exists("batteryreport.xml")) {
  46.             Write-Host "This device does not have batteries, or we could not find the status of the batteries."
  47.             Set-Asset-Field -Name "Block Battery Alert" -Value $true
  48.             exit 0
  49.         }
  50.         else {
  51.             [xml]$Report = Get-Content "batteryreport.xml"
  52.             foreach ($Batt in $Report.BatteryReport.Batteries.Battery) {
  53.                 $MaxCap = [int64]$Batt.FullChargeCapacity
  54.                 $NewCap = [int64]$Batt.DesignCapacity
  55.                 $Usable = [math]::round((($MaxCap * 100) / ($NewCap)), 2)
  56.        
  57.                 Write-Host ("Battery '" + $($Batt.id) + "' has a remaining maximum capacity of " + $Usable + "%.")
  58.        
  59.                 Log-Activity `
  60.                     -Message ("Battery ID '" + $($Batt.id) + "' is capable of " + $Usable + "% of its original capacity. " `
  61.                             + "The alert threshold was set to: " + $alertBelow + "%") `
  62.                     -EventName "Battery Health Check"
  63.        
  64.                 if ($Usable -lt $alertBelow) {
  65.                     Rmm-Alert `
  66.                             -Category 'Battery_Health' `
  67.                             -Body ("Battery ID " + $Batt.id `
  68.                                 + " on device " + $deviceName `
  69.                                 + " is only capable of " + $Usable `
  70.                                 + "% of its original capacity, less than the " + $alertBelow `
  71.                                 + "% threshold specified for this alert.")
  72.                     Set-Asset-Field -Name "Last Battery Alert" -Value $compThis.ToShortDateString()
  73.                 }
  74.             }
  75.         }
  76.     }
  77.     else { Write-Host "Battery Health Alert saturation threshold has not been reached. Exiting." }
  78. }
  79. else { Write-Host "Battery Health Alerts for this asset are permanently blocked. Exiting." }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement