Dynamic Output Variable In Script

We are running script on Octopus Server where we

  1. Add process step to read data from DB and based on deployment environment find machines id’s and create output variable for each machine (This of this as producer running on Octopus Server)
  2. Add process to consume output data generated in Step #1 and run it on deployment servers.

Step #1 looks something like

for ($i=0; $i -lt $machinesCount; $i++)
$dataJson= $dataJson[$i] | ConvertTo-Json
Set-OctopusVariable -name $machines[$i].Id -value $dataJson

Step #2 looks something like

$machineId = $OctopusParameters[“Octopus.Machine.Id”]
$octopusMachineId = “Octopus.Action[Producer].$machineId”

If I hard code machine id like “Octopus.Action[Producer].Output.Machines-277” I do see value. But I want to bind “Machines-277” dynamically based on where deployment is happening.


Write-Host $OctopusParameters[“Octopus.Action[Producer].Output.Machines-277”]

Write-Host $OctopusParameters[“Octopus.Action[Producer].Output.$machineId”]

How can I get output variable with dynamic value decided runtime? Please help me here.

Hi @davidkaren26,

Thanks for reaching out. I did a reproduction of the scenario on my local server and I was able to get it to work with the following syntax.

In one step named RegisterVariables, I created the output variable like this:

$machineId = $OctopusParameters[“Octopus.Machine.Id”]
Set-OctopusVariable -name "$($machineId)" -value "this worked"

In the step I want to reference the variable, I used this syntax:

$machineId = $OctopusParameters[“Octopus.Machine.Id”]
Write-Host $OctopusParameters[“Octopus.Action[RegisterVariables].Output.$($machineId)”]

The correct value was output for me when using the syntax above. I believe the issue stemmed from the variable $machineId being inside quotes. In PowerShell, a variable inside quotes needs to be referenced like the above.

Please let me know if this works for you or if you need more help with it.


Thank you so much ! I think $($machineId) did trick. Appreciate your kind help and response.

1 Like

You’re welcome! I’m glad it worked. Have a great weekend.


This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.