tags:

views:

1470

answers:

2

I'm trying to use the Microsoft.Sdc.Tasks.ServiceProcess.Exists to check whether or not a service exists. There is no example of using it in the documentation though. Anyone have one?

+1  A: 

I've not actually used this in production myself, and I'm not sure which version you have (I have a copy of Release 2.1.3155.0) and according to the accompanying .chm help file the Task has the following properties:

  • DoesExist Returns TRUE if the service specified exists
  • IsDisabled Returns TRUE if the service is disabled
  • ServiceName The short name that identifies the service to the system.

The ServiceName needs to be set to "The short name that identifies the service to the system, e.g. 'W3SVC'".

You might want to give it a try with a well known service (e.g. mssqlserver) and check the result of the other two properties (DoesExist/IsDisabled).

Update: Here's a sample (works):

Import the tasks, then call (e.g.)

< Microsoft.Sdc.Tasks.ServiceProcess.Exists ServiceName="Server"> < Output TaskParameter="DoesExist" PropertyName="Exists" /> < /Microsoft.Sdc.Tasks.ServiceProcess.Exists >

< Message Text="Service exists? $(Exists)" />

RobS
+2  A: 

This is how we check if service exists, stop it if so, do something, and start service again (if there was one and it was started).

Helper target:

<target name="service_exists">
 <script language="C#">
  <references>
   <include name="System.ServiceProcess.dll" />
  </references>
  <code><![CDATA[
   public static void ScriptMain(Project project) {
    String serviceName = project.Properties["service.name"];
    project.Properties["service.exists"] = "false";
    project.Properties["service.running"] = "false";

    System.ServiceProcess.ServiceController[] scServices;
    scServices = System.ServiceProcess.ServiceController.GetServices();

    foreach (System.ServiceProcess.ServiceController scTemp in scServices)
    {
     if (String.Compare(scTemp.ServiceName.ToUpper(), serviceName.ToUpper()) == 0)
     {
      project.Properties["service.exists"] = "true";
      project.Log(Level.Info, "Service " + serviceName + " exists");
      if (scTemp.Status.Equals(System.ServiceProcess.ServiceControllerStatus.Running))
       project.Properties["service.running"] = "true";
      project.Log(Level.Info, "Service " + serviceName + " is running: " + project.Properties["service.running"]);
      return;
     }
    }
    project.Log(Level.Info, "Service " + serviceName + " doesn't exist");
   }
  ]]></code>
 </script>
</target>

Usage:

<property name="service.name" value="Selection.Service" />
<call target="service_exists" />

<servicecontroller action="Stop" service="${service.name}" machine="${host}" timeout="60000" if="${service.exists}"/>

<!-- Do something -->

<servicecontroller action="Start" service="${service.name}" machine="${host}" timeout="60000" if="${bool::parse(service.exists) and bool::parse(service.running) == true}"/>

Hope I did not miss anything - our build admin keeps everything in one msbuild file which is now over 3600 lines :|

Dandikas