Variable scoping between Powershell scripts

We are deploying a service. I want to store the current path of the service executable in a variable with the PreDeploy script and use it in the DeployFailed script. I’m new to Powershell and I have no idea how you’re really calling each script, so will a variable declared & set in PreDeploy be visible to DeployFailed? If not, how would I go about doing this. Thanks.

Hi Jeff,

No data is kept between calling each of the deployment scripts (though this is a useful feature we will add).

For now the simplest way to do this would be to write the values to a file and read them from deployfailed.

Paul Stovell
Octopus Deploy
W: | T: @octopusdeploy

I did a little more research and learned about the PowerShell global variable scope.

I put this code at the start of the PostDeploy script:
$service = Get-Service $ServiceName -ErrorAction SilentlyContinue
$global:OldRCEServicePath = $service.PathName

And this in the DeployFailed script and it seemed to work:

$service = Get-Service $ServiceName -ErrorAction SilentlyContinue

if ( $service.PathName -eq $global:OldRCEServicePath)
    write-output "Deployment was halted before rollback was needed."
    write-output "Rolling back deployment to"
    write-output "Old RCE Service Path: " $global:OldRCEServicePath
    # more code

I’ve run into a similar scenario where I need some data passed from Deploy to DeployFailed. However using the global scope did not work, anything I place there is null in DeployFailed.


You can do this in Deploy.ps1:

Set-OctopusVariable -Name "Test" -Value "World!"

Then in DeployFailed.ps1, you can access it as $Test or $OctopusParameters["Test"].


Am I missing something? I am following your recommendation above and am unable to pass variable between scripts, here is my workflow:


Set-OctopusVariable -Name “Test2” -Value “World!”


write-host $OctopusParameters[“Test2”]

Nothing is being output in Deploy.ps1. Your help and insight would be much appreciated.

Sorry, I didn’t mean to close this. And sorry for not replying sooner. Those scripts are in the same NuGet package I assume? This should work.

I’ll look into why this doesn’t work and will make sure it works in Octopus 2.0. Thanks for the update.