How to get the Environment ID using Environment name

Hi Team,

We are trying to automate our Octopus CD process, we are creating Environment by providing the name, after creating that new environment I want to get that Environment ID and assign this value to another variable, so that I can use this variable to pass other scripts.
Please provide me some solution to get the Environment ID by using Environment name.

Regards,
Satish

Hi Satish,

Thanks for getting in touch! I believe the best would be to use the API. I have attached a small script which will take the input Environment name and output the Environment ID.

# You can this dll from your Octopus Server/Tentacle installation directory or from
# https://www.nuget.org/packages/Octopus.Client/

Add-Type -Path 'C:\MyScripts\Octopus.Client\Octopus.Client.dll' 

$apikey = 'API-1234567890abcdefghij' # Get this from your profile
$octopusURI = 'http://OctopusServer/' # Your Octopus Server address
$envName = 'Dev'

$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $octopusURI, $apiKey
$repository = New-Object Octopus.Client.OctopusRepository $endpoint

$envID = $repository.Environments.FindByName($envName).Id

$envId

Replace the values for $apikey, $octopusURI, and $envName.

We have some further documentation for our API which may also help in the future.

Let me know if you have any questions here or run into any issues.

Best regards,
Daniel

Hi Daniel,

Thank you very much for your help, It worked perfectly.

Regards,
Satish

1 Like

Hi Satish,

Thanks for the update! Iā€™m glad this helped.

Please feel free to get in touch at any time. :slight_smile:

Best regards,
Daniel

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