A PowerShell script that automatically removes offline agents from specified Azure DevOps agent pools. This tool helps maintain clean and efficient agent pools by removing inactive or disconnected agents.
In Azure DevOps, build and release agents can sometimes become offline or disconnected due to various reasons such as:
- VM rebuilds
- Network issues
- System updates
- Infrastructure changes
This script automates the cleanup process by:
- Connecting to specified Azure DevOps agent pools
- Identifying offline agents
- Removing them automatically
- Providing detailed logging of all operations
- PowerShell 5.1 or later
- Azure DevOps Personal Access Token (PAT) with the following permissions:
- Agent Pools (Read & Manage)
- Azure DevOps Organization name
- Clone this repository:
git clone https://github.com/yourusername/azdo-offline-agent-cleanup.git
- Navigate to the repository directory:
cd azdo-offline-agent-cleanup
- Create a
.env
file in the repository directory with the following variables:
PAT=your_personal_access_token
ORGANIZATION_NAME=your_organization_name
API_VERSION=5.1
POOLS_LIST=pool1,pool2,pool3
Variable | Description | Required | Default |
---|---|---|---|
PAT | Azure DevOps Personal Access Token | Yes | - |
ORGANIZATION_NAME | Your Azure DevOps organization name | Yes | - |
API_VERSION | Azure DevOps API version | No | 5.1 |
POOLS_LIST | Comma-separated list of pool names to check | Yes | - |
Run the script using PowerShell:
.\Remove-OfflineAgents.ps1
Checking Pool pool1 for Organization your_organization
Removing: agent1 From Pool: pool1 in Organization: your_organization
Removing: agent2 From Pool: pool1 in Organization: your_organization
No Agents found in pool2 for Organization your_organization
Remove-OfflineAgents.ps1
- Main script file.env.template
- Template for environment variables.env
- Your configuration file (not tracked in git).gitignore
- Git ignore configurationREADME.md
- Project documentation.devcontainer/
- Development container configuration for VS Code
This project includes a Dev Container configuration for Visual Studio Code, which provides a consistent development environment with all necessary tools pre-installed.
- Docker Desktop
- Visual Studio Code
- VS Code Remote - Containers extension
- Open the project in VS Code
- When prompted, click "Reopen in Container"
- Or use the command palette (F1) and select "Remote-Containers: Reopen in Container"
- VS Code will build and start the container with:
- PowerShell 7
- Required PowerShell modules
- Git
- Common development tools
- Consistent development environment across team members
- All required tools and dependencies pre-installed
- Isolated environment that doesn't affect your local system
- Easy onboarding for new contributors
-
Environment File Security:
- Never commit the
.env
file to version control - Keep your PAT secure and rotate it regularly
- Use minimum required permissions for the PAT
- Never commit the
-
Best Practices:
- Store the
.env
file securely - Regularly update the PAT
- Review agent removals in Azure DevOps audit logs
- Store the
The script includes comprehensive error handling for:
- Invalid credentials
- Network connectivity issues
- Missing or invalid pool names
- API request failures
- Environment file configuration issues
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
For support, please open an issue in the GitHub repository.
This project is licensed under the MIT License - see the LICENSE file for details.
- Azure DevOps REST API Documentation
- PowerShell Community