Get list of machines in a named role Octopus Deploy

I’m using Octopus Deploy and want to run a powershell script on machine a that calls an API with the names of the machines in a particular role.

Is it possible in a powershell step on machine with role-a to get a list of the names of the machines in role-b?

I’ve found Octopus.Environment.MachinesInRole[_role_] in the current docs but that appears to be an empty string


Thanks for reaching out!

That version of Octopus is really old by now (I didn’t even get to use it myself!), so I can’t guarantee the below snippet will work. But hopefully that part of the API hasn’t changed since then.


$OctopusAPIKey = "" #API key to authenticate with the Octopus API
$RoleFilter = "" #Role you want to filter for.


$OctopusBaseURL = $OctopusParameters['Octopus.Web.BaseUrl']

$EnvironmentID = $OctopusParameters['Octopus.Environment.Id']

$header = @{ "X-Octopus-ApiKey" = $OctopusAPIKey }

$AllMachines = (Invoke-WebRequest $OctopusBaseURL/api/environments/$EnvironmentID/machines -Headers $header | ConvertFrom-Json).items

$Machines = $AllMachines |?{$_.roles -icontains $RoleFilter}



I forgot to mention that the empty Octopus.Environment.MachinesInRole[\_role\_] is a bug we fixed a long time ago.

Do you have any plans on upgrading from that 2.4 instance?

I’d love to be able to upgrade from that instance :slight_smile:

I’ll try the script you provided and see what I find. Thanks

We have a guide to upgrade from 2.6 to the latest:

You’ll need to manually upgrade from 2.4 to 2.6 first.

Definitely something you want to take some time to plan though :slight_smile: