4

I'm new to msbuild and currently I'm trying to create msbuild script that will deploy my C# windows service to remote test server.

I'm thinking about using sc.exe utility for this purpose. Reading about it I didn't find a way to check whether windows service is installed on a remote server. If the service is installed then I need to stop it and update necessary files, otherwise I need to register the service.

P.S. For release builds I plan to use WiX to create MSI package.

lostaman
  • 922
  • 12
  • 25

2 Answers2

9

You need MSBuild Comminity Tasks. In latest build exists an example in MSBuild.Community.Tasks.v1.2.0.306\Source\Services.proj. It will solve first part of your question:

<PropertyGroup>
    <MSBuildCommunityTasksPath>$(MSBuildProjectDirectory)\MSBuild.Community.Tasks\bin\Debug</MSBuildCommunityTasksPath>
</PropertyGroup>

<Import Project="$(MSBuildProjectDirectory)\MSBuild.Community.Tasks\MSBuild.Community.Tasks.Targets"/>

<Target Name="Test">
    <CallTarget Targets="DoesServiceExist" />
    <CallTarget Targets="GetServiceStatus" />
    <CallTarget Targets="ServiceControllerStuff" />
</Target>

<Target Name="DoesServiceExist">
    <ServiceQuery ServiceName="MSSQLServer123" MachineName="127.0.0.1" >
        <Output TaskParameter="Exists" PropertyName="Exists" />
        <Output TaskParameter="Status" PropertyName="ServiceStatus" />
    </ServiceQuery>
    <Message Text="MSSQLServer Service Exists: $(Exists) - Status: $(ServiceStatus)"/>
</Target>

<Target Name="GetServiceStatus">
    <ServiceQuery ServiceName="MSSQLServer" MachineName="127.0.0.1">
        <Output TaskParameter="Status" PropertyName="ResultStatus" />
    </ServiceQuery>
    <Message Text="MSSQLServer Service Status: $(ResultStatus)"/>
</Target>

<Target Name="ServiceControllerStuff">
    <ServiceController ServiceName="aspnet_state" MachineName="127.0.0.1" Action="Start" />
    <ServiceController ServiceName="aspnet_state" MachineName="127.0.0.1" Action="Stop" />
</Target>

Those MSBuild task is just a wrapper around .Net class ServiceController. Take a look for documentation to understand how it works and how you can configure it in details.

Second part includes installing service. For that purpose sc.exe suits very well.

Community
  • 1
  • 1
Sergio Rykov
  • 4,176
  • 25
  • 23
  • Thanks. Have a question. Does service query can check the status of service on remote machine or local services only? – lostaman Feb 17 '11 at 15:46
  • I've updated default script and define supported parameter MachineName="127.0.0.1" to connect to the server. – Sergio Rykov Feb 18 '11 at 07:59
0

A complete solution is posted here. May help future visitors.

Update: Link updated as the other blogging service went down.

Mrchief
  • 75,126
  • 20
  • 142
  • 189
  • @Philter: Thanks for pointing out. I switched to cleaner Urls and thus you no longer need the .html extensions at the end. Updated link now. – Mrchief Feb 15 '13 at 04:52