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.
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.