Overview:
When managing multiple MongoDB Atlas projects within an organization, it often becomes challenging to keep track of all clusters - their configurations, regions, versions, and scaling details - across projects.
Instead of manually navigating through the Atlas UI, you can leverage the MongoDB Atlas Admin API and a simple PowerShell automation script to fetch this data programmatically.
In this guide, we'll build a PowerShell script that connects to the MongoDB Atlas Admin API, retrieves all projects in your organization, and exports detailed cluster information into a CSV (or Excel) file - all with a single command.
💡 Why Use PowerShell for MongoDB Atlas Automation?
- PowerShell provides a flexible scripting environment that works seamlessly with REST APIs.
- When combined with MongoDB Atlas Admin API, it allows administrators and DevOps engineers to:
- Fetch cluster and project data in bulk
- Automate reporting for governance or audits
- Simplify multi-project monitoring
- Integrate Atlas metadata into enterprise dashboards
🔑 Prerequisites
- Before running the script, ensure you have:
- MongoDB Atlas Programmatic API Keys
- Log in to MongoDB Atlas → Organization Settings → Access Manager → API Keys
- Note down your Public Key and Private Key
- Organization Access Level: The API key must have Organization Read Only or Organization Owner role.
# Define your MongoDB Atlas API credentials and organization ID
$publicKey = ""
$privateKey = "-79fd-4e02--"
$orgId = ""
# Base64 encode the API keys
#$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$publicKey:$privateKey"))
#Path to the csv & excel file to be saved
$csvFilePathAtlas = "D:\sampleset.csv"
# Delete file if exist
if (Test-Path $csvFilePathAtlas)
{
Remove-Item $csvFilePathAtlas
}
# Define array to collect all project cluster data
$exportRecords = @()
# Fetch projects
$projectsUrl = "https://cloud.mongodb.com/api/atlas/v1.0/groups"
#$projectsResponse = Invoke-RestMethod -Uri $projectsUrl -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
[securestring]$secStringPassword = ConvertTo-SecureString $privateKey -AsPlainText -Force
[pscredential]$credential = New-Object System.Management.Automation.PSCredential ($publicKey, $secStringPassword)
$projectsResponse = Invoke-RestMethod -Uri $projectsUrl -Headers @{Authorization = "Basic $base64AuthInfo"} -Credential $credential -Method Get
$projectsinfo = $projectsResponse.results
# Iterate over each project and fetch cluster details
foreach ($project in $projectsResponse.results) {
$projectId = $project.id
$projectName = $project.name
# Fetch clusters for the project
$clustersUrl = "https://cloud.mongodb.com/api/atlas/v1.0/groups/$projectId/clusters"
$clustersResponse = Invoke-RestMethod -Uri $clustersUrl -Headers @{Authorization = "Basic $base64AuthInfo"} -Credential $credential -Method Get
# Output project and cluster details
Write-Output "Project ID: $projectId"
Write-Output "Project Name: $projectName"
#Write-Output "Clusters:"
# Collect each cluster's info
foreach ($cluster in $clustersResponse.results) {
$record = [PSCustomObject]@{
ProjectID = $projectId
ProjectName = $projectName
ClusterName = $cluster.name
MongoVersion = $cluster.mongoDBVersion
Connectionstr = $cluster.connectionStrings.standardSrv
DiskSizeGB = $cluster.diskSizeGB
InstanceSize = $cluster.providerSettings.instanceSizeName
RegionName = $cluster.providerSettings.regionName
ProviderName = $cluster.providerSettings.providerName
Backuptype = $cluster.backupEnabled
Scaling = $cluster.autoScaling.diskGBEnabled
Pointintimes = $cluster.pitEnabled
}
# Add record to master array
$exportRecords += $record
}
}
$exportRecords | Select-Object ProjectID,ProjectName, ClusterName,MongoVersion, Connectionstr,DiskSizeGB, InstanceSize,RegionName,ProviderName, Backuptype,Scaling,Pointintimes | Export-Csv -Path $csvFilePathAtlas -NoTypeInformation
Write-Output "Exported ClusterInfo to file, please check the location!!!"
# Export the data to an Excel file
#$exportRecords | Export-Excel -Path "Project_ClusterInfo.xlsx" -WorksheetName "Cluster Info" -AutoSize
Top comments (0)