Using saved Variables in powershell that runs in machine context

I have set a coupe of variables in octopus then want to use them in my inline powershell script on a process.

But it is ran by a tentacle on the deployment machine. This doesn’t seem to be able use the variables. Is there any way to use them in this context? I want to be able to use it for API keys and such

Hi Andrew,

Thank you for reaching out to us with your query about variables.

You should be able to use variables in your inline script. It’d be great if we can check that the syntax is as expected and that the variables are scoped correctly. Could you please provide a code snippet and, if possible, a screenshot showing the variable configuration in Octopus Deploy? It’d also be really useful if you could confirm what currently happens (e.g. are the variables just ignored, is there an error message and so on).

Best Regards,

Charles

Hi Charles. Thank you for responding.

I’ve just been trying to use the convenience variable syntax so $varname
This is the config of the var I want to use.

Hope that helps

Hi Andrew,

Thank you for the screenshot.

It might be worth trying the $OctopusParameters["freshApiKey"] format for the moment, just to keep things simple. It’d also be good to check that the deployment you are running matches one or more of the scopes you have set for the variable (for example, check that you are deploying to “Development”).

If the above doesn’t help would you be able to confirm what happens when you try and use the variable? For example, do you see an error message in the task log?

Best Regards,

Charles

Hi Charles. Thank you. I have tried that format as well when I was just trying to work it out.

if I try to just echo out the var it just echos a blank space in the task log. There’s no error. The tentacle i’m using is a Listening tentacle if that helps.

Hi Andrew,

Thank you for getting back to me with this update.

One of the most common issues with variables is a scope mismatch. Would you be able to create a new variable, with no scope set, and try to access it in the script? I’m hopeful that the variable will work, but if it doesn’t it will at least help us to narrow down where the potential issue is.

Best Regards,

Charles

1 Like

Hi Charles

sorry for only just getting back to you. Removing all scope from my variable actually worked!!

Thank you for your help on this!

Andy

Hi Andrew,

That’s great to hear, I’m really pleased it’s working for you now.

Scopes can be tricky to narrow down so it’s likely there was a mismatch between the variable scope and the deployment target, environment, tag or so on that you were using. If you want to try adding the scopes back I’d suggest trying the broadest first (e.g. environment) and then gradually adding more restrictive scopes each time until you’ve honed in to where you want to be.

Please get back in touch with us if you have any questions.

Best Regards,

Charles

I do have a problem with it though

when I use this just to echo the value it echos out in the task log…

$apiKey = $freshApiKey
& echo "API Key: $apiKey"

but then when I go to use it as an arg to call a node script it’s sending as empty

& node "index.js" $projNum $apiKey

Hi Andrew,

Thank you for your message.

It looks like you are pushing the Octopus variable into the PowerShell $apiKey variable successfully, which would suggest that whatever is going wrong is happening in the script rather than as a result of something Octopus is doing. I’d be happy to try and advise further - can you post the whole script please?

Best Regards,

Charles

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