How can I remotely start / stop a service using MSBuild?

I would like to remotely start or stop the Windows service on another machine using MSBuild. For this, I wrote this script:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="C:\Program Files (x86)\MSBuild\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>
  <Target Name="MyTarget">
    <ServiceController MachineName="Box2" ServiceName="MyService" Action="Stop" />
  </Target>
</Project>

When I run this on a machine that can see Box2, I get the following:

Project "C: \ Scripts \ Test.xml" on node 1 (default targets).

C: \ Scripts \ Test.xml (4,5): error: Could not find "MyService" service on "Box2" Done Building Project

"C: \ Scripts \ Test.xml" (default goals) - FAILED.

I know that I have the correct service name (I copied and pasted it from the actual list of services), and I'm sure that it can see Box2, because if I change it to a machine name that does not exist (for example, Box2asdf) , it takes about 10 seconds to return (with the same error, mind you), unlike the almost immediate response that I get when I provide the correct machine name.

How can I debug this problem?

+3
source share
3 answers

Instead, you can try ...

You can use sc command line program and execute it ...

ie

SC \ ServerName stop ServiceName

http://support.microsoft.com/kb/166819

, msbuild, . msbuild

+2

. Sc, , . msbuild msbuild, sc exec? , DLL .

0

ServiceController Target ServiceController. , . , , , , - .

@jsobo , .Net :

sc.exe \ Box2 stop MyService

0
source

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


All Articles