Created
July 29, 2015 05:47
-
-
Save Damovisa/92dfad2935678298acae to your computer and use it in GitHub Desktop.
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
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory=$true,Position=1)] | |
[string]$OctopusUrl = "http://localhost:80", | |
[Parameter(Mandatory=$true,Position=2)] | |
[string]$ApiKey, | |
[Parameter(Mandatory=$false,Position=3)] | |
[switch]$Delete | |
) | |
Write-Host "Querying against $OctopusUrl with key $ApiKey\r\n" | |
# Octopus GET | |
Function Get-FromOctopus([string]$relUrl) { | |
$uri = "$OctopusUrl$relUrl`?apiKey=$ApiKey" | |
try { | |
return Invoke-RestMethod -Uri $uri -Method Get | |
} | |
catch { | |
if ($_.Exception.Response.StatusCode.value__ -eq 404) { | |
return $null | |
} else { | |
throw $_.Exception | |
} | |
} | |
} | |
# Octopus POST | |
Function Post-ToOctopus([string]$relUrl, $postObject) { | |
$deserialised = $postObject | ConvertTo-Json | |
return Invoke-RestMethod -Uri "$OctopusUrl$relUrl`?apiKey=$ApiKey" -Body $deserialised -Method Post | |
} | |
# Octopus DELETE | |
Function Delete-FromOctopus([string]$relUrl) { | |
return Invoke-RestMethod -Uri "$OctopusUrl$relUrl`?apiKey=$ApiKey" -Body $deserialised -Method Delete | |
} | |
### Put logic here ### | |
$projects = Get-FromOctopus -relUrl "/api/projects/all" | |
$projectsToDelete = @() | |
ForEach ($p in $projects) { | |
$name = $p.Name | |
$processLink = $p.Links.DeploymentProcess | |
$process = Get-FromOctopus -relUrl $processLink | |
if ($process -ne $null) { | |
Write-Host "Project '$name' has a process template with $($process.Steps.Count) steps" | |
} else { | |
$projectsToDelete += $p.Id | |
Write-Host "Project '$name' appears to be missing a process template and will be deleted" -foregroundcolor "red" | |
} | |
} | |
if ($Delete) { | |
ForEach ($p in $projectsToDelete) { | |
Write-Host "Deleting Project '$p'" -foregroundcolor "red" | |
Delete-FromOctopus -relUrl "/api/projects/$p" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
The above command will list all projects and highlight those that are missing processes as a result of an erroneous import. It will not delete them.
To actually delete, add another
-Delete
argument to the end of the command.