Search code examples
azure-devopswindows-servicesazure-devops-extensionsazure-devops-server-2019azure-devops-server

Azure DevOps On-Premise - How can I stop and delete a windows service on a remote machine?


I have an existing Windows service on a remote machine.

Here is what I need to do:

  1. Stop that service.
  2. Delete that service.
  3. Copy my build artifacts to the specified directory on that remote machine.
  4. Recreate that Windows service.
  5. Start the Windows service.

My problem:

  • My build agents can neither stop nor delete the existing Windows service.
  • I keep getting the following error:

    [SC] OpenSCManager FAILED 5:

    Access is denied.


enter image description here


Solution

  • @Daniel Mann asked the right question.

    First of all,

    sc STOP $(serviceName)
    

    should have been a red flag.

    @Daniel Mann asked:

    Does the service account that your agent is running under have appropriate permissions [to do this]?

    But one should also ask:

    Which machine are you looking to execute this against? The build agent?

    This was, in fact, my problem. When I tested this release pipeline the first time, the target machine and the build agent just happened to be the same machine.

    This was an unfortunate accident, because this was not to be the case for all releases.


    Solution:

    This is correct:

    sc \\$(serviceMachine) STOP $(serviceName)
    

    And @Daniel Mann was correct:

    • The service account that the build agent is running under must have the appropriate permissions to do this.