Trying to stop/uninstall/install/start an apache server via PowerShell deployment script fails with NativeCommandError

Hello everyone,

I am trying to stop, uninstall, re-install and start an apache service with PowerShell deployment scripts, but this causes problems. The background of why I am doing it this way is that our deployment process is to stop and uninstall affected services, then modify files, and finally re-install and re-start them.

For this purpose, I have created the following PowerShell script. This script works reliably when manually run on the deployment targets:

.\NServer -k stop -n NApacheServer 2>&1 | %{ "$_" } 
.\NServer -k uninstall -n NApacheServer 2>&1 | %{ "$_" } 

However, when run as a PowerShell deployment step during an Octopus Deploy release, the following error is logged, and the release is cancelled:

Executing script on 'NDevServer' 
NServer.exe : The 'NApacheServer' service is stopping. 
At C:\Octopus\Work\20180317043159-7217-5\Script.ps1:5 char:16 
+ .\NServer <<<<  -k stop -n NApacheServer 2>&1 | %{ "$_" } 
+ CategoryInfo          : NotSpecified: (The 'NApa...ce is stopping.  
:String) [], RemoteException March 17th 2018 13:32:06Error    + FullyQualifiedErrorId : NativeCommandError 
The remote script failed with exit code 1 
The action Stop and Uninstall Services on NDevServer failed 

The funny thing here is that the service is actually stopped, and the only actual problem is that Octopus Deploy stops the deployment at this point.

I was eventually able to find a somewhat cumbersome workaround by wrapping the entire thing in a try-catch as follows:

    .\NServer -k install -n NApacheServer
    .\NServer -k start -n NApacheServer
catch [System.Management.Automation.RemoteException]{
    if ($_.TargetObject -like "Connecting to *" -and $_.CategoryInfo.Category -eq "NotSpecified" -and $_.FullyQualifiedErrorId -eq "NativeCommandError" -and $_.InvocationInfo.MyCommand.Name -like "psexec*.exe"){

This causes a warning to shop in the deployment logs of Octopus Deploy, but at the very least, the functionality works now.

My main point here is that the following PowerShell script works when executed directly on the Deployment Target, but not when executed via Octopus Deploy:

.\NServer -k stop -n NApacheServer 2>&1 | %{ "$_" } 
.\NServer -k uninstall -n NApacheServer 2>&1 | %{ "$_" } 

Initially, the problem also occurred on the deployment target, but I was able to fix this by appending "2>&1 | %{ “$_” } " to the faulting lines, which caused the feedback strings provided by Apache (which were incorrectly recognized as errors) to be printed as normal strings instead. However, for some reason, this does not work with Octopus Deploy, and it took me quite some time to figure out a working solution.

If someone could look into why this does not work with Octopus Deploy, that would be great.

Thanks in advance,
Kira Resari

Hi Kira,

It looks like this could be due to a bug in PowerShell, where if $ErrorActionPreference is set to Stop (which we do in our bootstrap scripts) it will halt execution of a script if a message is written to the error stream, even if it’s being redirected.

Could you try to add `$ErrorActionPreference = ‘Continue’ and see if that allows the execution to continue?

Thank you and best regards,

Hi Henrik,

thanks for the reply.

I added this line, together with my initial PowerShell fix, and now it works perfectly. It doesn’t even display a warning in Octopus anymore.

The final code now looks like this:

$ErrorActionPreference = ‘Continue’

.\NServer -k stop -n NApacheServer 2>&1 | %{ "$_" } 
.\NServer -k uninstall -n NApacheServer 2>&1 | %{ "$_" } 

Thanks for the help!


Hi Kira,

Great to hear that you got past your issue.

Thank you and best regards,

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