You need MSBuild Comminity Tasks . In the latest build, there is an example in MSBuild.Community.Tasks.v1.2.0.306 \ Source \ Services.proj . It will solve the 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>
These MSBuild tasks are just a .Net class ServiceController shell. Take a look at the documentation to understand how it works and how you can customize it in detail.
The second part includes the installation of the service. For this, sc.exe is suitable for very well .
source share