Removing Agents with no contact over 90 days

I am a user not an employee. I like to program in POWERSHELL which is easier for me to understand.
The following is a small Powershell script which removes remote agents that have not contacted in the last 90 days.
Before you begin you need the API Token (create it under the settings - read/write permissions) And you need to get windows Powershell 7 or above. Replace the token with your own and replace the server URL with your own.

##################################################
#Netbeez delete agents over 90 days
#You should run it with Powershell 7 or higher.
##################################################
$Token = "123123123123123123123" | ConvertTo-SecureString -AsPlainText -Force
$date1 = get-date "1/1/1970"
$date2 = (get-date).adddays(-90)
$datets = [math]::truncate((new-timespan -start $date1 -end $date2).totalseconds * 1000.0)
$Uri = "https://netbeez.domain.com/agents?type=beta&filter[active]=false&filter[active_ts][operator]=<&filter[active_ts][value]=$datets"
$agentstr = invoke-restMethod -uri $Uri -method "GET" -Authentication "Bearer" -Token $Token
#Displays a list of the agents to delete
$agentstr.data
#if empty do nothing, otherwise start deleting
if ($agentstr.data.count -gt 0) {
   foreach ($agent in $agentstr.data) {
      $aid=$agent.id
      $url = "https://netbeez.domain.com/agents/"+$aid+"?type=beta"
      $result = invoke-restMethod -uri $url -method "DELETE" -Authentication "Bearer" -Token $Token
      $result
      }
   }
else {
   write-host "No agents found"
   }
pause
#The End
2 Likes

Hi @joeatffcu! Thank you for your contribution and welcome to the community! I’m sure this will be useful to a lot of users.