endpoints/Alerts.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<#
  .Synopsis
  Get all Atera alerts
#>

function Get-AteraAlerts {
  return New-AteraGetRequest -Endpoint "/alerts"
}

<#
  .Synopsis
  Get an alert based on it's ID
#>

function Get-AteraAlert {
  param(
    # ID of Alert to retrieve
    [Parameter(Mandatory)]
    [int]$ID
  )
  return New-AteraGetRequest -Endpoint "/alerts/$alertId"
}

<#
  .Synopsis
  Filter alerts based on status and severity
   
  .Parameter Open
  Turn on to retrieve Open alerts
 
  .Parameter Closed
  Turn on to retrieve Closed alerts
   
  .Parameter Information
  Turn on to retrieve alerts with Information severity
   
  .Parameter Warning
  Turn on to retrieve alerts with Warning severity
   
  .Parameter Critical
  Turn on to retrieve alerts with Critical severity
   
  .Example
  Get-AteraAlertsFiltered -Open -Critical
  # Retrieve all open critical alerts
 
  .Example
  Get-AteraAlertsFiltered -Open -Warning -Critical
  # Retrieve all open alerts that have either warning or critical severity
#>

function Get-AteraAlertsFiltered {
  param(
    # Get Open alerts
    [switch] $Open,
    # Get Closed alerts
    [switch] $Closed,
    # Get Information alerts
    [switch] $Information,
    # Get Warning alerts
    [switch] $Warning,
    # Get Critical alerts
    [switch] $Critical
  )

  return Get-AteraAlerts | Where-Object {
    if ($Open.IsPresent -and $_.Archived) { return $false}
    if ($Closed.IsPresent -and !$_.Archived) { return $false }

    if ($Information.IsPresent -and $_.Severity -ne "Information") { return $false }
    if ($Warning.IsPresent -and $_.Severity -ne "Warning") { return $false }
    if ($Critical.IsPresent -and $_.Severity -ne "Critical") { return $false }
    return $true
  }
}

<#
  .Synopsis
  Creates a new Atera alert
   
  .Parameter DeviceGuid
  ID of the agent to open alert against. Can be retrieved from Atera Agents endpoint
  .Parameter CustomerID
  Customer ID to open alert in.
  .Parameter Title
  Title to give the alert
  .Parameter Severity
  Severity of the alert. Defaults to Warning. Options: Information, Warning, Critical
  .Parameter AlertCategoryID
  Category of alert. Defaults to General. Options: Hardware, Disk, Availability, Performance, Exchange, General
  .Parameter AlertMessage
  Additional information to show with alert in dashboard
  .Parameter TicketID
  Ticket that alert is assigned to
  .Parameter Code
  ?
  .Parameter ThresholdValue1
  Optional field to store alert information. Only available through API
  .Parameter ThresholdValue2
  Optional field to store alert information. Only available through API
  .Parameter ThresholdValue3
  Optional field to store alert information. Only available through API
  .Parameter ThresholdValue4
  Optional field to store alert information. Only available through API
  .Parameter ThresholdValue5
  Optional field to store alert information. Only available through API
  .Parameter SnooozedEndDate
  If alert is snoozed tracks when to notify about the alert
  .Parameter AdditionalInfo
  Optional field to store alert information. Only available through API
  .Parameter MessageTemplate
  ?
  .Parameter FolderID
  ?
   
  .Example
  $BatteryReport = Join-Path $env:TEMP "batteryreport.xml"
  $Proc = Start-Process -FilePath powercfg.exe -ArgumentList "/batteryreport /xml /output $($BatteryReport)" -PassThru -NoNewWindow -Wait
 
  if ($Proc.ExitCode -eq 1) {
    Write-Host "No battery present"
    exit
  }
 
  $Report = ([xml](Get-Content $BatteryReport)).BatteryReport
 
  $MaxCharge = $Report.Batteries.Battery.FullChargeCapacity
  $DesignCapacity = $Report.Batteries.Battery.DesignCapacity
  $BatteryHealth = $MaxCharge / $DesignCapacity
 
  if ($BatteryHealth -gt 0.5) {
    Write-Host "Battery is charging to $([math]::Round($BatteryHealth*100, 2))% of designed capacity"
    exit
  }
  # Create the alert saving the charge percentage in Threshold Value 1, the Max Charge in Threshold Value 2 and the Designed capacity in Threshold Value 3
  Get-AteraAgent | New-AteraAlert -Title "Battery unhealthy" -Severity Warning -AlertCategoryID Hardware -ThresholdValue1 $BatteryHealth -ThresholdValue2 $MaxCharge -ThresholdValue3 $DesignCapacity
#>

function New-AteraAlert {
  [CmdletBinding()]
  param (
    [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
    [string] $DeviceGuid,
    [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
    [int]$CustomerID,
    [Parameter(Mandatory)]
    [string] $Title,
    [ValidateSet("Information","Warning","Critical")]
    [string] $Severity = "Warning",
    [ValidateSet("Hardware","Disk","Availability","Performance","Exchange","General")]
    [string] $AlertCategoryID = "General",
    [string] $AlertMessage,
    [int] $TicketID,
    # ?
    [int] $Code,
    # ?
    [string] $ThresholdValue1,
    # ?
    [string] $ThresholdValue2,
    # ?
    [string] $ThresholdValue3,
    # ?
    [string] $ThresholdValue4,
    # ?
    [string] $ThresholdValue5,
    [DateTime] $SnoozedEndDate,
    [string] $AdditionalInfo,
    # ?
    [string] $MessageTemplate,
    # ?
    [int] $FolderID
  )
  New-AteraPostRequest -Endpoint "/alerts" -Body $PSBoundParameters
}