Could you add Octopus.Deployment.MachineNames


(Fabian) #1

Hello
I’d like to find out the name of all machines that are part of a deployment inside an Octopus Powershell script.
You provide a variable called Octopus.Deployment.Machines
However this gives me just the machine ids . e.g. machines-123,machines-124 not the names

I have written a script to get me the names but it seems a bit cumbersome because it seems i need to pass in an API key to connect to the octopus endpoint .

My questions are:

  • could you provide a system variable called Octopus.Deployment.MachineNames ?
  • is there a way to get the machine name by machineid without having to connect to an endpoint?
  • if there isn’t , is there a way to connect to the end point using the credentials of the user who is signed in to the octopus web app instead of an API key.

Here’s my script

Add-Type -Path “C:\Program Files\Octopus Deploy\Tentacle\Octopus.Client.dll”

$octopusURI = ‘https://xxxxx/’ # Your server address
$apikey = “xxxxxxxx”

$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $octopusURI,$apikey
$repository = New-Object Octopus.Client.OctopusRepository $endpoint
$machineIds = $OctopusParameters[‘Octopus.Deployment.Machines’].Split(’,’)

$machineNames = “”
foreach ($machineId in $machineIds) {
$machine = $repository.Machines.Get($machineId.trim())
$machineNames = $machineNames + $machine.Name + “;”
}
Write-Host $machineNames


(Kenneth Bates) #3

Hi Fabian,

Thanks for getting in touch! That’s great to hear you’ve written a script to get the machine names, though I do get your concern here that hitting the endpoint (and having to use an API key) isn’t ideal in this case. Thank you for sharing your idea on how to improve Octopus, and I’d suggest adding this idea up on our UserVoice page to allow us to gauge community support for this enhancement, and to prioritize it accordingly.

https://octopusdeploy.uservoice.com/

As an alternative, you could possibly take advantage of the system variable Octopus.Machine.Name, and store that in an output variable. Something like Set-OctopusVariable -name "MachineName" -value "#{Octopus.Machine.Name}". When this step is run on multiple machines, multiple values scoped to each machine are created with its own value for the machine name. You can then call the machine-scoped output variable using the syntax Octopus.Action[StepA].Output[Machine].MachineName.

An example very similar to this is outlined in the following blog post.

This concept could potentially be expanded and maybe provide a viable, non-API-key-using solution for the time being. Would something like that be helpful and more ideal?

I look forward to hearing your thoughts, and don’t hesitate to reach out if you have any further questions moving forward. :slight_smile:

Best regards,

Kenny


(Fabian) #4

Hi Kenny

I need to know all machine names in a deployment not just the current one. What I am actually doing is taking a machine off the load balancer. Before i do that i want to make sure that at least one other machine is still online. So i need to know all machines in a deployment.

Could you answer one of my other questions?
Is there a way to connect to the end point using the credentials of the user who is signed in to the octopus web app instead of an API key?

Thank you.
Fabian


(Kenneth Bates) #5

Hi Fabian,

Thanks for following up. I believe it’s possible to connect to the endpoint with the current user’s web portal credentials by hitting the /integrated-challenge URL. This will cause an authentication cookie to be stored on the response, and using Octopus.Client it’d look something like this sample snippet I found:

var repo = new OctopusRepository(new OctopusServerEndpoint("[your url}"));
repo.Client.Get("/integrated-challenge");
// now authenticated

I hope this helps! Let me know how you go or if you have any other questions going forward.

Best regards,

Kenny


(Fabian) #6

Any chance you have this code in powershell. i tried this but didn’t work

$OctopusURL = “http://abc.com/
$endpoint = new-object Octopus.Client.OctopusServerEndpoint $OctopusURL
$repository = new-object Octopus.Client.OctopusRepository $endpoint
$repository.Client.Get("/integrated-challenge")