Get a list of target machine hostnames in a powershell step

Dennis Dyall's Avatar

Dennis Dyall

07 Dec, 2017 12:41 PM

Opening this question based on a previous one who has almost the exact same problem.
The user andrewsav is looking to get a list of names and hostnames to the targets of the deployment. He needs this list to run to load-balancer checks prior to deploying.
The community rep suggested using the OctopusDeploy REST Api. But that requires a lot of overhead.

I want to have something like this:
Write-Host $OctopusParameters['Deployment.Machine.Hostname'] server1.example.com,server2.example.com

There is a variable called Octopus.Machine.Hostname but it only gives you the single name of the current target being deployed to.
Why don't you provide an option to show me all of them? Is there a Powershell command I can use? Get-MachineHostname -MachineId "Machine-123" ?

Is this really the best we can do?
`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"

https://help.octopusdeploy.com/discussions/questions/12709-get-a-li...

  1. 1 Posted by Dennis Dyall on 07 Dec, 2017 02:34 PM

    Dennis Dyall's Avatar

    Actually, typo:
    Write-Host $OctopusParameters['Deployment.Hostnames'] server1.example.com,server2.example.com

  2. Support Staff 2 Posted by Henrik Andersso... on 08 Dec, 2017 12:48 AM

    Henrik Andersson's Avatar

    Hi Dennis,

    Thanks for getting in touch.

    Unfortunately this is the only way to currently achieve what you want.

    We don't provide a list of machine names in the variables only a list of machine ids as you've already found. To improve on this, I've raised this GitHub issue to allow filtering the results from the machines API endpoint by a comma-separated list of machine ids, and I'm working on implementing it as we speak.

    This issue does not include providing a variable that contains the list of machine names, but it means that you don't have to call the API for each machine instead you just need to call the API once and then extract the machine name from the resulting list that is returned.

    My sincere apologies for the inconvenience caused by this issue.

    Thank you and best regards,
    Henrik

  3. 3 Posted by Dennis Dyall on 10 Dec, 2017 04:49 PM

    Dennis Dyall's Avatar

    Hello and thanks for your kind response!

    Oh, I was hoping Octopus would set a variable containing the a list of the
    machines being deployed to. Surely this must be more convenient than using
    the REST Api? I have to create a Octopus rest api client in each of my
    steps just get this simple information?

  4. Support Staff 4 Posted by Henrik Andersso... on 11 Dec, 2017 11:22 PM

    Henrik Andersson's Avatar

    Hi Dennis,

    I responded to your comment on the GitHub issue, but I'll also include it here.

    After having a chat with the team, we have decided that we will add a new variable that will contain the machine names as well. (I had a vague recollection that we didn't do this for a reason, but I have not been able to find said reason.)

    Thank you and best regards,
    Henrik

  5. 5 Posted by Dennis Dyall on 12 Dec, 2017 12:12 PM

    Dennis Dyall's Avatar

    Thank you very much, Henrik! Det var snällt. It will help me and others a
    lot hopefully :)

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac