Get all projects with specific environment and role

Hello guys,

I’m trying to sort out how to list projects which have releases on specific environment and on specific target role.
Example: Get a list of all projects which are deployed on STAGE and on the target machine with role WebserverExternal.

Add-Type -Path 'Octopus.Client.dll' 

$apikey = 'API-*********' 
$octopusURI = 'http://octopusserver.domain.com' 
$environment = "STAGE"
$role = "WebserverExternal"

$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $octopusURI,$apikey 
$repository = New-Object Octopus.Client.OctopusRepository $endpoint
$project = $repository.Projects.FindByName($projectName)
$process = $repository.DeploymentProcesses.Get($project.DeploymentProcessId)
$environmentToAdd = $repository.Environments.FindByName($environment)

$webClient = New-Object System.Net.WebClient
$webClient.Headers.add('accept','application/json')
$webClient.Headers.add('X-Octopus-ApiKey',$apikey)
$webClient.Headers.add('X-NuGet-ApiKey',$apikey)
$allRoles = $webClient.DownloadString($octopusURI+'/api/machineroles/all?ApiKey='+$apikey)

Any help will be much appreciated.

Kind Regards,
B

Hi @bdjurkic,

Thanks for getting in touch! I had a look at this and whilst technically anything like this is possible via the API, what you are after here is fairly complicated.

Octopus stores this information in separate sections and I do not believe there is any easy way to match and only retrieve projects which meet this criteria.

Unfortunately I do not believe we will be able to provide much help here. It may be possible that others have written something similar though so I am happy to leave this conversation open.

Sorry for the bad news. :slightly_frowning_face:

Best regards,
Daniel