Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ==============================================================================
- # == Created by Jeremy McMahan, McMahan TECH LLC. 2022-01-15 Rev. 1.0 ==
- # == Revision Notes: ==
- # == 1.0: Initial Version. ==
- # ==============================================================================
- # Script Variables to pass: =
- # deviceName: Platform variable {{asset_name}} =
- # lastBatteryAlert: Platform {{asset_custom_field_last_battery_alert}} =
- # blockBatteryAlert: Platform {{asset_custom_field_block_battery_alert}} =
- # saturationDays: Integer limiting how often an alert will be raised. =
- # alertPercent: Integer setting allowable battery health % before alerting. =
- # =
- # Prerequisites: =
- # A) You must have a custom asset field titled "Last Battery Alert" =
- # of type "date" to store last alert date for saturation limiting. =
- # B) You must have a custom asset field titled "Block Battery Alert" =
- # of type "check box" to store systems not to check or alert on. =
- # the output message to write to. =
- # C) To really leverage this script, create an automated remediation to =
- # trigger an email to the client offering to replace their battery or =
- # create a ticket etc. =
- # =
- # ==============================================================================
- # ==============================================================================
- Import-Module $env:SyncroModule
- if (($lastBatteryAlert -eq $null) -or ($lastBatteryAlert -eq "")) { $lastBatteryAlert = (Get-Date).AddDays(-$saturationDays) }
- $alertBelow = [System.Double]$alertPercent
- # Check if Battery Health Alerts are disabled and bypass script.
- if (!($blockBatteryAlert -eq "yes")) {
- $compLast = $lastBatteryAlert
- $compThis = Get-Date
- $tSpan = (New-TimeSpan -Start $compLast -End $compThis)
- # Check if the saturation threshold is reached and bypass remainder of script.
- if ($tSpan.Days -ge $saturationDays) {
- # Do the check.
- & powercfg /batteryreport /XML /OUTPUT "batteryreport.xml"
- start-sleep 3
- if (![System.IO.File]::Exists("batteryreport.xml")) {
- Write-Host "This device does not have batteries, or we could not find the status of the batteries."
- Set-Asset-Field -Name "Block Battery Alert" -Value $true
- exit 0
- }
- else {
- [xml]$Report = Get-Content "batteryreport.xml"
- foreach ($Batt in $Report.BatteryReport.Batteries.Battery) {
- $MaxCap = [int64]$Batt.FullChargeCapacity
- $NewCap = [int64]$Batt.DesignCapacity
- $Usable = [math]::round((($MaxCap * 100) / ($NewCap)), 2)
- Write-Host ("Battery '" + $($Batt.id) + "' has a remaining maximum capacity of " + $Usable + "%.")
- Log-Activity `
- -Message ("Battery ID '" + $($Batt.id) + "' is capable of " + $Usable + "% of its original capacity. " `
- + "The alert threshold was set to: " + $alertBelow + "%") `
- -EventName "Battery Health Check"
- if ($Usable -lt $alertBelow) {
- Rmm-Alert `
- -Category 'Battery_Health' `
- -Body ("Battery ID " + $Batt.id `
- + " on device " + $deviceName `
- + " is only capable of " + $Usable `
- + "% of its original capacity, less than the " + $alertBelow `
- + "% threshold specified for this alert.")
- Set-Asset-Field -Name "Last Battery Alert" -Value $compThis.ToShortDateString()
- }
- }
- }
- }
- else { Write-Host "Battery Health Alert saturation threshold has not been reached. Exiting." }
- }
- else { Write-Host "Battery Health Alerts for this asset are permanently blocked. Exiting." }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement