Skip to content

Instantly share code, notes, and snippets.

@Dalmirog-zz
Created November 16, 2015 18:42
Show Gist options
  • Select an option

  • Save Dalmirog-zz/6c20de5e49696b446ce6 to your computer and use it in GitHub Desktop.

Select an option

Save Dalmirog-zz/6c20de5e49696b446ce6 to your computer and use it in GitHub Desktop.
GetAllMachinesFromDeployment
$APIKey = "" <-#API Key to authenticate to your Octopus server. This is the only parameter you must fill manually.
function GetMachines([string]$APIKey){
$OctopusURL = $OctopusParameters["Octopus.web.baseurl"]
$IDs = $OctopusParameters["Octopus.Deployment.Machines"]
$MachineIds = $IDS.Split(",")
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
}
}
}
foreach($ID in $MachineIds){
Get-FromOctopus -relUrl "/api/machines/$ID"
}
}
$machines = GetMachines -APIKey $APIKey
###Your logic goes here
# $Machines will be an array of machine objects. To see all the properties of these objects browse /api/machines/all
#The line below will return the name of the machines in Octopus
$machines.Name
#The line below will return the urls with which the machines were registered in Octopus
#$Machines.uri
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment