I’m using Octopus Deploy 2.4.7.85 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
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.
###Config###
$OctopusAPIKey = "" #API key to authenticate with the Octopus API
$RoleFilter = "" #Role you want to filter for.
###Process###
$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}
$Machines.Name