MSbuild Check if Windows service is installed

I am new to msbuild and am currently trying to create an msbuild script that will deploy my Windows C # service to a remote test server.

I am thinking of using the sc.exe utility for this purpose. Reading about this, I did not find a way to check if the Windows service is installed on the remote server. If the service is installed, I need to stop it and update the necessary files, otherwise I need to register this service.

PS To create releases, I plan to use WiX to create the MSI package.

+4
source share
2 answers

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 .

+9
source

The complete solution is published here . May help future visitors.

Refresh . The link has been updated as another blog service has been disabled.

0
source

Source: https://habr.com/ru/post/1340099/


All Articles