Passing Library Variables to Powershell

We have a central machine that runs all of our powershell scripts. From that machine, we would normally run a script from a powershell window as such:

./deploy-product.ps1 –App Appname –Environment Dev –Deploy –BatchSize 5 –DrainF5
or:
./deploy-product.ps1 –App Appname –Environment Test –Deploy –BatchSize 5 –DrainF5
or:
./deploy-product.ps1 –App Appname –Environment Prod –Deploy –BatchSize 5 –DrainF5

Essentially what I’m trying to do is get Octopus deploy, as a web front end, to call these powershell scripts on the remote machine. It works great, using the following deployment process: Run a PowerShell script across machines in roles: admin

d:\apps\scripts\deploy\deploy-product.ps1 –App Appname –Environment Dev –Deploy –BatchSize 5 –DrainF5

What I would like to do is use a Library Variable set to specify the “-Environment” variable in the command above, like the following:

Name: Value: Scope:
Env -Environment Prod machine1; admin; Prod PS
Env -Environment Test machine1; admin; TestPS
Env -Environment Dev machine1; admin; Dev PS

I’d like to call the correct environment, depending on which Environment is selected for the release, as specified by the Env variable. The following, and various other things I’ve tried, doesn’t work, and I don’t seem to be able to pass the Env variable to the powershell script:

d:\apps\scripts\deploy\deploy-product.ps1 –App Appname $env –Deploy –BatchSize 5 –DrainF5

Is there a way to pass a tentacle.exe parameter, that will recognize the $env global Library variable to the powershell script?

Thanks in advance.

Hi Darin,

Have you included the library variable set containing the Env variable into the appropriate project (and then created a new release to capture it)? Defining a variable called Env that is scoped to the environment should be fine.

You can also use:

$OctopusParameters['Octopus.Environment.Name']

…in PowerShell to get the name of the current environment.

Cheers,
Nick

We upgraded to 2.5.8.447, and at first we were able to pass our project variable for environment to an embedded powershell script in the package (basically, to test which environment it is deploying to in order to determine whether to fully execute the embedded deploy.ps1 file or not). After our first successful release test, it appears that the value is no longer passing at all for subsequent releases. Restarting the tentacle service on the deployment machine did not have any effect. Is there anything we may be missing, or any other recommendation?

Hi Sam,

Are you able to send a deployment log of the release that did expand the variable, and one of the subsequent ones that did not.


This should help us figure out why this is not passing the variable.

Vanessa

Hi Vanessa,

I don’t think this is an issue anymore; it looks like a change in
roles/variable scope caused the issue for us during testing. Thanks for the
article on getting the raw output logs!

Sam