Remotely deploying a Windows Service

2016-03-28

Remote deploy a Windows service

If you’re developing applications for Windows then you will know what services application components that can run the background. Unfortunately, deploying a service isn’t so straightforward, especially if you need to deploy it to multiple machines in one go.

In this guide, you will learn the following:

  • Install the service with the command line
  • Use a network driver to download the service from the development machine
  • Run a PowerShell script to restart the service

Install/uninstall a Windows Service

To install a service from the command line, use the InstallUtil.exe program:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe C:\path\to\service.exe

To uninstall a service, you can you the sc command line program like so:

sc delete servicename

servicename can be found in the properties window of the Services utility app.

Start/stop the service from the command line

To start the service:

net start servicename

Other available commands:

  • net pause
  • net continue

Using a network driver

The net command is used to manage File Shares and can be very convenient to make the service files available to all development machines on the same network. Start by using net view to list all the visible machines on the network.

Sharing a folder

To share a folder (usually the bin\Debug folder):

net share sharename=path\to\folder /grant:everyone,READ

Make sure the share has been registered:

net share

Stop sharing a folder

net share sharename /delete

Map a network drive to a shared folder on target devices

With the desired folder now being shared, you need a way to easily access those from other computers. This is where the net use command comes into play:

net use k: \\JAMESVM\kservice /p:yes

Check the newly created network drive is now returned in net use.

Copy a folder from a network drive to C:

To copy a folder on Windows, use the xcopy command:

xcopy "k:\source\folder" c:\destination/folder /s /I

Source: https://www.youtube.com/watch?v=ns8Mhd3-KU0

Network driver for UWP

Mapping to a network drive:

net use u: \\JAMESVM\uwp

Executing commands remotely

Download PsExec from here: https://technet.microsoft.com/en-us/sysinternals/psexec.aspx

Required setup on remote machine

To enable psexec, run this command on the target machine:

reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\system /v LocalAccountTokenFilterPolicy /t REG_DWORD /d 1 /f

Run the cmd command on the source machine

psexec \\HPELITEX2_008 -u LocalUser -p p@ssw0rd cmd

Stop the service remotely

psexec \\HPELITEX2_008 -u LocalUser -p p@ssw0rd -h net stop servicename

Readings

Further readings

comments powered by Disqus