Forked from Damovisa/Delete-ProjectsWithoutProcesses.ps1
Last active
November 1, 2015 04:52
-
-
Save Dalmirog-zz/11e7ab6e03466b426a7c to your computer and use it in GitHub Desktop.
This file contains 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 | |
) | |
Add-Type -Path "Octopus.Client.dll" | |
$endpoint = new-object Octopus.Client.OctopusServerEndpoint "$($OctopusURL)","$($APIKey)" | |
$repository = new-object Octopus.Client.OctopusRepository $endpoint | |
### Put logic here ### | |
$projects = $repository.Projects.Findall() | |
$projectsToDelete = @() | |
ForEach ($p in $projects) { | |
$name = $p.Name | |
if (![string]::IsNullOrEmpty($p.DeploymentProcessId)) { | |
$process = $repository.DeploymentProcesses.Get($p.links.deploymentprocess) | |
Write-Host "Project '$name' has a process template with $($process.Steps.Count) steps" | |
} else { | |
$projectsToDelete += $p | |
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.name)" -foregroundcolor "red" | |
$repository.Projects.delete($p) | |
} | |
} |
This file contains 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: