What is the best way of automating Windows Service deployment?

13,687

I would do the bulk of the work from the powershell script.

  1. Use the msbuild Exec task like you mentioned to add it to your build process. Here's a pretty good article on using the exec task to run your powershell script.

  2. Since you are using VS 2010, a setup and deployment project is pretty easy. Add one to your solution, and add the output of your service project to it. Here's an article on adding the setup project for a windows service.

  3. Use your powershell scripts to copy the installer .msi to the remote server. You can simply use copy-item [source] [destination] if you have access to the file share.

  4. Stop the service on the remote machine. You can use (get-service -ComputerName [destination] -Name [service-name]).Stop() (from this question)

  5. Install the service silently using psexec psexec \\remotecomputer "[msi-destination-path]" /qn Here's the rest of the command line options for the .msi.

  6. Start the service using (get-service -ComputerName [destination] -Name [service-name]).Start()

I'd also add a bunch of parameters to the powershell script for destination server, service name, etc. It will make maintaining this part of your build process much easier. Your build agent will most likely have to be an administrator on the destination machine as well.

Lastly, make sure to put your powershell build script in source control!

Edit (June 2014)
VS 2013 has installer projects again! (Sorry, VS 2012)

Also, if found this awesome answer about how to install a Windows Service without using a setup project.

Share:
13,687

Related videos on Youtube

Dour High Arch
Author by

Dour High Arch

Updated on September 15, 2022

Comments

  • Dour High Arch
    Dour High Arch over 1 year

    I have created a windows service using C# in Visual Studio 2010. I did a lot of research about automating the installation process. Got lots of advice but none did work for me. The windows service i have created has lots of dependencies and i would like client to have no UI interaction during the installation. I have created a setup project for this which includes all my dependencies within the installer.

    Process involved:

    • Create a build for windows service
    • Push the setup file (.msi) to remote location
    • Call the .msi and install the service silently without user interaction.

    What i did so far:

    • Created a powershell script to push files to remote location
    • Execute powershell script and install the service

    Please keep in mind that powershell script

    sc create "servicename" binpath="somepath"
    

    is used for installing service from a project directory not for installing .msi file created using Setup Project these are two vast different things. If you don't understand this don't answer please.

    Possible solutions:

    Now my question is. How can i push windows service installer to remote location? And how can create custom build arguments and use either msbuild extension pack or Exec Task and install the service?

    I know this sounds stupid and irritating question for someone who hasn't installed the service using (.MSI) created by the Setup Project. But its my requirements which i am trying to solve since weeks in the enterprise environment.