Roopesh Shenoy
Roopesh Shenoy

Reputation: 3447

MSBuild - Deploying a Windows Service Automatically after building

Ok, this is what I want to do -

We have a windows service and we want to automate deployment of this service to the test environments after every successful CI build - for this we thought of the following approach -

However, currently I am facing problems with the starting and stopping of the windows service.

This is the MSBuild snippet -

<!-- TODO: do this using Powershell instead of normal commandline so that it can be executed on remote machines as well -->
<Exec Command="Net Stop CreditProcessing" />
<!-- Exec Command="$(PowerShellExe) -NoProfile -Noninteractive -command &quot;&amp;{ Stop-Service CreditProcessing }&quot;" /-->

<!-- TODO: The destination needs to come from the env variable or rsp file? -->
<Copy 
    SourceFiles="@(WindowsServiceFilesToDeploy)" 
    DestinationFiles="@(WindowsServiceFilesToDeploy->'D:\WindowsServices\Credit Processing Service%(RecursiveDir)%(Filename)%(Extension)')" />  

<Exec Command="Net Start CreditProcessing" />   
<!--Exec Command="$(PowerShellExe) -NoProfile -Noninteractive -command Start-Service CreditProcessing" /--> 

This I tried with both direct command line commands as well as powershell commands.

A) This is the problem I face when I try to run this build with normal commands - the service takes some time to start/stop and the build doesn't wait long enough and reports an error and stops

"... service could not be started". "Service did not report an error"

or sometimes when stopping

"The service could not be controlled in its present state."

whereas if we immediately check the services it will be in "Starting" stage and soon it will go to "Start" stage. Any way to make the build be a little more patient? The command doesn't do this if run directly from command prompt.

B) This is the problem we face if I use the above powershell commands (currently commented in the snippet) -

It just opens the powershell prompt in the build log and then just does nothing Build stops at powershell doesn't continue

Can you help avoid these problems (at least one of these)? I think the powershell route is more recommended if it works fine, but I'm open to suggestions.

A couple of other questions -

Upvotes: 2

Views: 980

Answers (2)

JohnL
JohnL

Reputation: 4002

Your command to launch is of this form:

path\to\powershell.exe
-arg1 -arg2

The newline means that PowerShell never receives the arguments, so doesn't know it has to execute a Start-Service command, and doesn't know you don't want it to be interactive. It's probably just waiting for you to tell it what to do.

Check your $(PowerShell) variable, as it probably has a newline at the end.

Upvotes: 1

Lo&#239;c MICHEL
Lo&#239;c MICHEL

Reputation: 26150

why not use sc.exe which has native remote capabilities ?

Upvotes: 1

Related Questions