Last active
March 20, 2025 08:24
-
-
Save ruant/5781e9852933855163829198f9137096 to your computer and use it in GitHub Desktop.
Octopus Deploy - Fetch all tenants and their projects, export to TAB separated (tsv) file
This file contains hidden or 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
# Define variables | |
$octopusURL = "OCTOPUS_URL" # Adjust based on your setup | |
$apiKey = "API_KEY" # Adjust based on your setup (create one under your account in octopus) | |
$spaceName = "Default" # Adjust based on your setup | |
$amountOfTenantsToFetch = 10000 | |
# Define headers for authentication | |
$headers = @{ | |
"X-Octopus-ApiKey" = $apiKey | |
"Content-Type" = "application/json" | |
} | |
# Get space ID | |
$spaces = Invoke-RestMethod -Uri "$octopusURL/api/spaces" -Headers $headers | |
$space = $spaces.Items | Where-Object { $_.Name -eq $spaceName } | |
$spaceId = $space.Id | |
# Get all tenants - MODIFIED FOR DEBUGGING: only get one tenant | |
$tenantsUrl = "$octopusURL/api/$spaceId/tenants?take=$amountOfTenantsToFetch" | |
$tenants = (Invoke-RestMethod -Uri $tenantsUrl -Headers $headers).Items | |
# Prepare output for Excel | |
$output = @() | |
$output += "Tenant Name`tProject Name" # Header row | |
foreach ($tenant in $tenants) { | |
# Check if tenant has any projects | |
$hasProjects = ($tenant.ProjectEnvironments.PSObject.Properties.Name | Measure-Object).Count -gt 0 | |
if ($hasProjects) { | |
# The ProjectEnvironments property contains a dictionary of project IDs as keys | |
foreach ($projectId in $tenant.ProjectEnvironments.PSObject.Properties.Name) { | |
# Get project details | |
$projectUrl = "$octopusURL/api/$spaceId/projects/$projectId" | |
$project = Invoke-RestMethod -Uri $projectUrl -Headers $headers | |
$output += "$($tenant.Name)`t$($project.Name)" # Tab-separated values | |
} | |
} else { | |
# Add tenant with empty project name | |
$output += "$($tenant.Name)`t" | |
} | |
} | |
# Define the output file path | |
$outputFilePath = Join-Path -Path (Split-Path -Parent $PSCommandPath) -ChildPath "octopus_tenant_projects.tsv" | |
# Write output to TSV file | |
$output -join "`r`n" | Out-File -FilePath $outputFilePath -Encoding UTF8 | |
# Display confirmation message | |
Write-Host "Tenant projects data has been written to $outputFilePath" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment