I made a script to delete duplicated atera agents

support-info Member Posts: 9
edited December 2023 in Scripts

At the time I was not able to find an Atera feature to delete duplicated agents (based on the device serial number) so I made a litle script

I'm sharing it here for thoses who might need it
(it's not perfect, I would be happy to receive feedbacks on it)

(Please note that the script has only been tested on powershell 7)

The script does the following:
1. Get All the Atera agents
2. Go through all the agent and only keep the one that match specifics parametters:
- Os Type not "Server"
- Agent Serial NB not empty
- Agent Last Logged user not empty
- Agent Last Logged user not in white list defined in $SKIP_SPECIFIC_USER
3. Find all the duplicated agents and add every one of them to the deletion list based on the last seen date
(For Exemple if 3 agents have the same Serial Number, the two agents with the farthest last Seen date will be added to a list)
4. This list is then exported as a CSV FILE
5. If the parameter $SEND_LOG_TEAMS is set to true, this list will be sent formated as an adaptive card to a Teams chanel using the Teams webhook
6. If the parameter $AUTO_DELETE is set to true, the script will try to delete every agent in the list.
7. A csv log file containing every deletion atempt is also created

You can set the script to delete the duplicated agents automaticaly or only send you the list of duplicated agents so you can review them manualy

The Teams adaptive card message looks like this: