Get a list of target machine names in a powershell step

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.

How do I do that?

Hi,

Thanks for getting in touch!

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:

$hostnameinput = "#{Octopus.Machine.HostName}"
write-host $hostnameinput

When I run this script from my Octopus server (and not my Tentacles) it returns the two machine names of my Tentacles that were in the role I defined.

I would also recommend reading up on output variables that can be used in Octopus, you can find the list of available system variables here.

Another alternative would be to install and use OctoPosh’s Get-OctopusMachine cmdlet.

Please let me know if this is not what you are looking for or if you need any further assistance,

Regards,

Alex

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?

Hi,

That is absolutely possible! If you use the Octopus.Machine.Name variable it will return the result that you are looking for.

Using the following powershell:

write-host #{Octopus.Machine.Name}
write-host #{Octopus.Machine.HostName}
write-host #{Octopus.Deployment.Machines}

I get the following response:

Tentacle-SRV2012-01
alexrolley450d
Machines-161

The first response is the name of the machine on the environments page, the second is machines hostname and the third is the machine ID.

Please let me know if there is anything else I can help with,

Happy deploying!

Alex

Hello Alex,

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.

Thanks,
Andrew

Hi Andrew,

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:

write-host #{Octopus.Action[Step1].Output.machineName}

Gave me an output of:

SRV2012_01 SRV2016_01 SRV2016_02

For the three machines that were the chosen role for the scipt step defined in Step1.

Please let me know if you have any issues or if there is anything else I can help you with,

Regards,

Alex

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.