Last active
August 29, 2015 14:19
-
-
Save ChaseFlorell/b1ef0b0d7a21f374aac3 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
$apiKey = "Your API Key" | |
$OctopusURL = "Your Octopus URL" | |
$Header = @{ "X-Octopus-ApiKey" = $apiKey } | |
#Getting all machines given an Environment name | |
$EnvironmentName = Read-Host 'What environment do you want to wipe out?' | |
Write-Warning "You are about to remove ALL machines from the $EnvironmentName environment." | |
$confirm = Read-Host 'Are you sure you want to continue? Y/N' | |
if($confirm = 'Y'){ | |
$Environment = Invoke-WebRequest -Uri "$OctopusURL/api/Environments/all" -Headers $Header| ConvertFrom-Json | |
$Environment = $Environment | ?{$_.name -eq $EnvironmentName} | |
$Machines = (Invoke-WebRequest -Uri "$OctopusURL/api/Environments/$($Environment.id)/machines" -Headers $Header| ConvertFrom-Json).items | |
#Deleting all machines found on the environment | |
$total = $Machines.Count | |
for ($i=0; $i -lt $Machines.Count; $i++){ | |
$percentComplete = (($i/$total)*100) | |
Write-Progress -activity "Deleting Machines in the $EnvironmentName environment." -percentComplete $percentComplete -currentOperation "Deleting $($($Machines[$i]).Name). [$percentComplete %] Complete." | |
#Running a DELETE agains the machine link will delete it from Octopus | |
Invoke-WebRequest "$OctopusURL$($($Machines[$i]).Links.Self)" -Headers $Header -Method Delete | Out-Null | |
} | |
Write-Host "Finished deleting $total machines from the $EnvironmentName environment." -f Green | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment