Powershell request

(SACCO) #1

Hello,

I am new on Octopus Deploy. According to me, it’s very interessting.

Now, I am looking for for a PowerShell request to list machines of an environment. I’ve tried this:
$repository.Environments.FindByName(‘MyEnv’) |gm but I don’t find any related property nor Method.

Could you help me, please.

Thank You in advance.

(SACCO) #2

I’ve found this request:
$repository.Machines.FindAll() |Where-Object {$_. EnvironmentIds -eq “MyEnvId”} |select name

This supposes to know your envID.

If you have other ideas …

(Matt Richardson) #3

Hi there

Thanks for getting in touch!

To get all the machines in a given environment, you can do something like this:

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

$apikey = 'API-1234' # Get this from your profile
$octopusURI = 'https://youroctopus.com' # Your server address
$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $octopusURI,$apikey 
$repository = New-Object Octopus.Client.OctopusRepository $endpoint

$env = $repository.Environments.FindByName("MyEnv")

$machinesInEnvironment = ($repository.Machines.FindMany({ param($m) $m.EnvironmentIds -contains $env.Id }))

That will return a list of all the machines that are in that environment.

From a more general point of view, you can learn a lot from our samples repository - there’s often a script in there that is very similar to what you’re trying to do.

Another useful reference is the source code for OctopusClients, which can help you figure out what methods you can call.

Hope that helps!

Regards,
Matt

(SACCO) #5

Hello Matt,

Excellent. Thank You so much.

1 Like
(system) closed #6

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.