Get DeploymentTarget of each step in powershell script

I wolud like to get in a powershell script the DeploymentTarget on each Action. I added a step with a powershell script, but i miss the deploymentTarget info :
#{each action in Octopus.Action}

// Do some stuff
//need to get the deploymentTarget where the action is run
//Something like 
 #{each target in action.DeploymentTarget}
     // get the target machine name


Hi gcalabro84,

Welcome to the Octopus forums!

I believe you can do what you are trying to achieve with system variables. For example, to get the machine name you can use our system variable Octopus.Machine.Name, or to get the machine ID targeted by a deployment you can use Octopus.Deployment.Machines

Here are some documents on all of our System Variables:

Please let me know if I misunderstood the question or if you need more help.


Hi jeremy,
thank you for the support.
I also try with the system variables, but it’s no sufficent,because each action in my case can have different targets,
Action 1 -> Machine1,Machine2
Action2 -> Machine3
and i need , foreach action, to know who is the target machine.
I found a default output variable -> ‘action.Output’.
Is it correct ?


Hi gcalabro84,

I’m sorry I’m not certain about the process you’re describing. Are you wanting to reference these machines before/after the actions actually take place? Could you please describe your process in greater detail and I will try to do my best to figure out a solution for you.


Hi Jeremy,
I’ll describe the process as best i can :
During a release different processes are take place. Each process can have different deployment targets.
at the end of all process excecution a power shell script will run.
The script will cycle all the executed actions and i need to get, foreach action the deployment targets.
After some attemps i can have the information inside the action.Output value, but it’s just a string with the name of the deployment target.
I’d like to get more informations, for example the type (Offline Package Drop,Windows etc )…
How can i get these informations ?
Thank you for support.

Hi Gabriele,

Thanks for the greater detail. I think I’ve got a potential solution for your first question, but it will require a small bit of logic. It will be a two part step, you will first need to gather which role(s) are being targeted by an action with Octopus.Action.TargetRoles, then you will need to output the machines in the role with Octopus.Environment.MachinesInRole[\_role\_]. The complication will come in when you have multiple roles being targeted by an action. You will need to include logic to iterate through all of the target roles and run the 2nd command for each role that was targeted. Links to the specific spots for these 2 commands are here and here.

For your second question, if you mean step type, you can get more information about a particular action by using API calls to drill down into your deployment process and filter based on the information you’d like to get. Here is a PowerShell script for cloning a deployment process. We don’t want to clone the deployment process, but it has the logic in there to get access to the deployment process to parse through the data. The easiest way to figure out exactly how the JSON you’re pulling down is structured would be to take a look at the JSON in your web browser by just going to the URL that you do the API call with, for example https://yourserverurl/api/projects/all . You can then see what sort of information in the deployment process is there for you to gather. If it was more information about the machine you want to gather, you can also get more information about a particular machine from API calls. You can examine that information by navigating to http://yourserverurl/api/machines/all

Please let me know if this will work for you or if you need further assistance.


Hi Jeremy.
Thank you for support, it works well.
But just a question. Why an API key is necessary also if the script run in the octopus server ?
Thank you again

Hi Gabriele,

The API Key is necessary because the PowerShell script runs in an independent isolated process and has to make API calls the same as if you are running them on a separate machine. If you do not want to hard-code it into the script, you can make the API key a sensitive project variable and insert it into the script. Please let me know if you have any questions.


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