I would like to write a powershell script step (running on the octopus server) that would check if the current deployment targets are in the correct load balancer pool before proceeding with the rest of the deployment. For that I need to get a list of the current deployment targets, and not in a form of machine IDs (Machine-10,Machine-11), but the actual machine names.
We have an inbuilt variable that should give you the answer that you are looking for, Octopus.Machine.HostName. For example, you could do the following in Powershell:
Thank you for this. Is it possible to get the name as can be seen on the http://octopus/app#/environments page? These can differ from the hostname. I need to get it from a powershell script step of a process. There is a variable Octopus.Deployment.Machines but that returns Mahcine-11, Machine-12 not the names I can see on the http://octopus/app#/environments page. May be I’m missing the correct variable?
thank you for your help with that. If I’m not mistaken, what you are suggesting returns a single name. What I’m after is the list of current deployment targets. The script is running on the Server not on deployment targets themselves.
Sorry about the back and forth on this one, however I believe I have come up with a solution for you!
I’ve created a powershell script step named Step1 that is run on my Octopus server, however it is running on behalf of a target role (to define the list of machines to retrieve) and I have created a blank Octopus variable called machineName.
The script step then has the following code:
Add-Type -Path 'C:\Program Files\Octopus Deploy\Octopus\Octopus.Client.dll'
$apikey = 'API-123456789YK92VBEXFWGYJC4' # Replace with your own API Key. You can generate one from your profile
$octopusURI = 'http://Octopus.Server' # Your server address
$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $octopusURI,$apikey
$repository = New-Object Octopus.Client.OctopusRepository $endpoint
$machineIds = $OctopusParameters['Octopus.Deployment.Machines'].Split(',')
$tempArray = @()
foreach ($machineId in $machineIds) {
$machine = $repository.Machines.Get($machineId)
$tempArray += $machine.Name
}
Set-OctopusVariable -name "machineName" -value "$tempArray"
That will write an array of Machine Names to the Octopus variable machineName. To use these values in a following step you can use our Output Variable syntax, for example:
Right, thank you. I conclude that it means that there is no system variable for this. I would suggest adding it then. Something like Octopus.Deployment.MachineNames. This would not require usage of output variables or separate step and you will have this information at your fingertips. You also won’t need the API key.
Another suggestion is to have a more natural way of calling Octopus.Client when inside a build step. That is a way that would not require the API key, because it’s really the server that is executing this. Since you have your bootstrap script anyway, that’s where all the magic could happen.
Just a suggestion, anyway. Thank you again for your continued help.