Fire and Forget PowerShell script step

We have a step in our deployment process that restarts a Windows Service and on occassion the restart can take hours to complete. This step needs to occur as part of the deployment process. (It’s a restart of a Windows Service which itself uses a RavenDb database–need I say more?)

Anyway, it’s not important to the deployed application, however, that the service is up and running immediately after the app has been deployed. It just needs to be restarted; if it fails to restart, we have other monitoring in place to detect that condition.

I have some PowerShell code in a step action template, but when we simulate a condition where restart of the service would fail, Octopus Deploy is reporting errors even after I have gone through the trouble to use Start-Process and $WarningActionPreference = 'SilentlyContinue'! It’s very aggravating. Here’s the PowerShell I’m using:

$null = Start-Process PowerShelle.exe -ArgumentList "-NoLogo", "-NonInteractive", "-Command", "$WarningActionPreference = 'SilentlyContinue'; Restart-Service MyServiceName -WarningAction 'SilentlyContinue'" -NoNewWindow

As you can see, I’m trying very hard to not have any warnings displayed. I’ve gone through the trouble of using Start-Process, swallowing the output (so Octopus can’t implicitly capture it) so that the script will immediately return and Ocotpus would just “move on”. But this is not happening, and Octopus is still waiting around (the step takes about a minute to execute, while that line of PowerShell code on my machine takes less than one second!) and showing warnings emitted from the code executing in a spawned process. What’s going on? How can I prevent this?

Actually, this is not an Octopus Deploy issue at all. I finally managed to see this happen on the server running the command in a PowerShell Window.

And, to close the loop on this, apparently, even though I used $null = Start-Process PowerShell.exe -ArgumentList <myArgs> -NoNewWindow, thinking it would just start a process and return me to the prompt and nothing else would come of it…when there was an issue restarting the service which the spawned PowerShell process attempted, it reported an error back to the console anyway, which is what Octopus was showing me in the deployment log. To prevent this, I needed to add the following to my command:

$null = Start-Process PowerShell.exe -ArgumentList "-NoLogo", "-NonInteractive", "-Command", "Restart-Service MyService -WarningAction 'SilentlyContinue' -ErrorAction 'SilentlyContinue'" -NoNewWindow

Then nothing is reported back to the console (and consequently, to the Octopus Deploy logs). And the step only took 22 seconds to execute instead of 1m+. Perfect.

Hi there,

I’m glad to hear you found your way around it :slight_smile: . Just in case anyone else runs into something similar: Start-Job might be a better fit in this scenario. It basically starts a background Job in a Fire and Forget way. It even gives you a Job ID which you can use to retrieve the output of that job in case you need to use it later on (perhaps you want to store it in a DB and have a background job that monitors your background job’s outputs and setup alerts for them).

MSDN Documentation for that cmdelt:


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