Created
February 15, 2019 08:33
-
-
Save MisterDuval/221a789a31df4e585d938425ccb87843 to your computer and use it in GitHub Desktop.
Atera : Dead client removal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
## ORIGINAL POST : https://support.atera.com/hc/en-us/community/posts/360002992288-Atera-s-Support-Team-Hack-of-the-week-Delete-Offline-Agents-using-API- | |
# | |
####### Edit this section only ########## | |
$apiKey = "XX" # your API key , get it from: https://app.atera.com/Admin#/admin/api | |
$offlinePeriodIndays = 60 | |
$deleteAgents = 0 # set to 1 only after you verify the list | |
####### edit this section only ########## | |
###################################################################################### | |
## Please do not alter the script below, it can cause a real damage to your account ## | |
###################################################################################### | |
try { | |
$last = 0 | |
$inactiveAgents = @() | |
$pageLink = 'https://app.atera.com/api/v3/agents?itemsInPage=50' | |
$monthAgo = (Get-Date).AddDays(0 - $offlinePeriodIndays) | |
while (!$last) { | |
$request = Invoke-RestMethod -Headers @{"X-API-KEY" = $apiKey } -Method GET -Uri $pageLink -ContentType application/json | |
$inactiveAgents += $request | Select-Object -Expand items | where { $_.Online -eq 0 -and [DateTime]$_.Modified -lt $monthAgo } | |
if ($request.nextLink.Length -gt 0) { | |
$pageLink = $request.nextLink; | |
Start-Sleep -s 1 | |
} | |
else { | |
$last = 1 | |
} | |
} | |
if (!$deleteAgents) { | |
#see the agents | |
$inactiveAgents | select AgentName, IpAddresses, OSType, CustomerName, Modified, Online | Sort-Object -Property AgentName | ft | |
$agentsCount = $inactiveAgents.Count | |
Write-Host "$agentsCount agents were found in the last $offlinePeriodIndays days" | |
Write-Host "###This is just a test, please verify if these are the agents you want to delete.###`nOnce verified, set the deleteAgents parameter to 1 (at the top of this script)." | |
Break; | |
} | |
$retryCount = 0 | |
foreach ($agent in $inactiveAgents) | |
{ | |
$retryCount = 0 | |
while ($retryCount -lt 3) { | |
$retryCount += 1 | |
try { | |
$url = "https://app.atera.com/api/v3/agents/$($agent.AgentId)"; | |
Invoke-RestMethod -Headers @{"X-API-KEY" = $apiKey } -Method DELETE -Uri $url -ContentType application/json | |
write-host "$($agent.AgentName) Deleted :)" | |
Start-Sleep -s 1 | |
Break | |
} | |
catch { | |
Start-Sleep -s 6 | |
if ($retryCount -eq 3) { | |
write-host "$($agent.AgentName) Delete Agent Failed. Error: $_.Exception" | |
try { | |
$streamReader = [System.IO.StreamReader]::new($_.Exception.Response.GetResponseStream()) | |
$ErrResp = $streamReader.ReadToEnd() | |
$streamReader.Close() | |
} | |
catch { | |
} | |
} | |
} | |
} | |
} | |
} | |
catch { | |
if ($_.Exception.Response.StatusCode -eq "Unauthorized") { | |
Write-Host "Failed to authenticate, make sure the correct api-key has been added" | |
} | |
else { | |
Write-Host "Failed to get your account agents list" | |
Write-Host "error:" $_.Exception | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment