Home > front end >  Powershell script: pull data json data, check value (date) of a object, if older than x days, execut
Powershell script: pull data json data, check value (date) of a object, if older than x days, execut

Time:02-16

{
  "items": [
    {
      "AlertID": 4369,
      "Code": 52,
      "Source": "AlphaAgentSelfMonitoring",
      "Title": "CPU-Temperatur ( (CPU Core #1), (CPU Core #2) und (CPU Package))",
      "Severity": "Critical",
      "Created": "2022-02-15T07:32:45Z",
      "SnoozedEndDate": null,
      "DeviceGuid": "*",
      "AdditionalInfo": null,
      "Archived": true,
      "AlertCategoryID": "Hardware",
      "ArchivedDate": "2022-02-15T07:55:38Z",
      "TicketID": null,
      "AlertMessage": "*",
      "DeviceName": "SERVER",
      "CustomerID": 23,
      "CustomerName": "*",
      "FolderID": null,
      "PollingCyclesCount": null
    },
    {
      "AlertID": 4368,
      "Code": 12,
      "Source": "AgentsOnlineStatusCheckLoop",
      "Title": "Machine status unknown",
      "Severity": "Critical",
      "Created": "2022-02-15T05:27:03Z",
      "SnoozedEndDate": null,
      "DeviceGuid": "*",
      "AdditionalInfo": null,
      "Archived": true,
      "AlertCategoryID": "Availability",
      "ArchivedDate": "*",
      "TicketID": null,
      "AlertMessage": "*",
      "DeviceName": "*",
      "CustomerID": 23,
      "CustomerName": "*",
      "FolderID": null,
      "PollingCyclesCount": null
    },

Hi, I can pull this json alert data like this from Atera API. If I want to delete this alert I just need the AlertID of said alert.

I know how to pull data, however I need some kind of loop that would check the date of alert. Basically:

  1. go through json data
  2. check Created value
  3. if value > 3 days
    • then attach value of AlertID to url and invoke-restmethod, else
    • nothing

If anyone could help with this or tell me where to look for more specific instructions in powershell it would be great, thank you very much for reading.

Update: This is the working code. It could be cleaner, but it is what it is. It throws some errors but it works as intended:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$Headers = @{
    "accept" = "application/json"
    "X-API-KEY" = "somekey"
  }
$Uri = "https://app.atera.com/api/v3/alerts?alertStatus=Open"
$alerts = Invoke-RestMethod -Uri $Uri -Method Get -Headers $Headers
$refDate = (Get-Date).AddDays(3).Date  
$alerts.items | Where-Object { [datetime]$_.Created -lt $refDate } | ForEach-Object {
    $AID = $_.AlertID
    $DELETEURI = "https://app.atera.com/api/v3/alerts/"   $AID
    $senddelete = Invoke-RestMethod -Uri $DELETEURI -Method Delete -Headers $Headers
    
}

CodePudding user response:

Using your example (only I have changed the Created dates in order to see the output), you can simply do this:

$json = @"
{
  "items": [
    {
      "AlertID": 4369,
      "Code": 52,
      "Source": "AlphaAgentSelfMonitoring",
      "Title": "CPU-Temperatur ( (CPU Core #1), (CPU Core #2) und (CPU Package))",
      "Severity": "Critical",
      "Created": "2022-02-11T07:32:45Z",
      "SnoozedEndDate": null,
      "DeviceGuid": "*",
      "AdditionalInfo": null,
      "Archived": true,
      "AlertCategoryID": "Hardware",
      "ArchivedDate": "2022-02-15T07:55:38Z",
      "TicketID": null,
      "AlertMessage": "*",
      "DeviceName": "SERVER",
      "CustomerID": 23,
      "CustomerName": "*",
      "FolderID": null,
      "PollingCyclesCount": null
    },
    {
      "AlertID": 4368,
      "Code": 12,
      "Source": "AgentsOnlineStatusCheckLoop",
      "Title": "Machine status unknown",
      "Severity": "Critical",
      "Created": "2022-02-10T05:27:03Z",
      "SnoozedEndDate": null,
      "DeviceGuid": "*",
      "AdditionalInfo": null,
      "Archived": true,
      "AlertCategoryID": "Availability",
      "ArchivedDate": "*",
      "TicketID": null,
      "AlertMessage": "*",
      "DeviceName": "*",
      "CustomerID": 23,
      "CustomerName": "*",
      "FolderID": null,
      "PollingCyclesCount": null
    }
    ]
}
"@ | ConvertFrom-Json

# 3 days ago, set to midnight
$refDate = (Get-Date).AddDays(-3).Date  
# now filter on items where the date in the 'Created' property is older than the reference date
$json.items | Where-Object { [datetime]$_.Created -lt $refDate } | ForEach-Object {
    # here you combine the $_.AlertID with your URL and perform the Invoke-RestMethod
    # for demo, just output the ID
    $_.AlertID
}

Output for this JSON:

4369
4368
  • Related