How can I remotely install or uninstall a service on Windows Server 2008 R2?

2

1

Who knows a simple solution how to install or uninstall a Windows service remotely on a Win2k8R2 server?

This server is a hosted at the internet, so it's not in a domain or such. Thus I want to avoid the use of windows file sharing or administration services.

I would prefer a possibility where I can trigger the execution of a server-side script which installs already uploaded service-binaries.

You're welcome to name any tools or .NET code solutions if you know how to accomplish that.

Edit: Sorry, I have to clarify a bit, it's not as easy as using powershell or scripting with InstallUtil in my case. I'll try it with bullet points:

  • I want to install a service unattendedly by invoking the service installation on a server, running on the internet, from a client.
  • I don't have a GUI from the server while invoking the service install. For example, I trigger a setup.exe, which installs the services itself via SSH (self-installing service to make use of custom service names). Not having a GUI seems to be a problem(?).

I even tried desperately to invoke the service setup program via a php web service (shell_exec), but the result is always the same: Setup is executed, but no services are installed.

To the client it should be as easy as possible to invoke the service installation on the server, without seeing anything of it. It's for a periodic and automated deployment of some service-based applications we create.

derFunk

Posted 2011-09-20T14:42:55.380

Reputation: 151

This sounds like a programming question, perhaps it should be on stackoverflow instead of superuser? – Harry Johnston – 2011-09-20T21:17:45.970

Is the installer (setup.exe) your own code? – Harry Johnston – 2011-09-20T21:19:08.760

Its our own installer, yes, and here at superuser I'm actually looking for a non-programming answer (although I could handle that as well ;) ). I'm quite certain it's possible to remote-install a windows service on an internet host unattendedly. Or prove me wrong ;) – derFunk – 2011-09-21T08:06:29.683

From what you've said, though, the problem seems to be that your setup.exe doesn't work properly when invoked remotely. Fixing this problem would be a programming question. If you're looking for an existing solution, I don't understand what you want that solution to do other than run setup.exe, which you've already tried. – Harry Johnston – 2011-09-22T21:17:32.317

The installer is not doing anything else than what can be done via command line. So forget the setup.exe for now. My most urgent question is: Is there any way to remotely install a windows service on an Win2k8 Server machine running on the internet? – derFunk – 2011-10-12T11:45:13.143

Well, it depends on the service. Have you tried using the sc create command from a server-side script? – Harry Johnston – 2011-10-13T22:36:09.243

Answers

2

Powershell seems like a likely option. Take a look at the cmdlet reference and the Add-WindowsFeature cmdlet

uSlackr

Posted 2011-09-20T14:42:55.380

Reputation: 8 755

Powershell is the way of the future. Even Microsoft has started creating all their diagnostic tools on top of Powershell. – surfasb – 2011-09-20T16:33:32.437

Its a server machine running on the internet, I wouldn't open windows file sharing services to the public here. Is powershell still an option now? Anyone really tried already to remotely install a service on an internet server? (every tunnel thinkable is possible). – derFunk – 2011-10-12T11:47:11.453

Powershell v2's remoting service runs over an ssh-like connection. – uSlackr – 2011-10-12T12:56:17.643

2

I did this recently using PowerShell and WMI. Full details here but in a nutshell something like the below works, as well as PowerShell remoting or SC.exe

function Install-Service(
[string]$serviceName = $(throw "serviceName is required"),
[string]$targetServer = $(throw "targetServer is required"),
[string]$displayName = $(throw "displayName is required"),
[string]$physicalPath = $(throw "physicalPath is required"),
[string]$userName = $(throw "userName is required"),
[string]$password = "",
[string]$startMode = "Automatic",
[string]$description = "",
[bool]$interactWithDesktop = $false
)
{        
# todo: cleanup this section
$serviceType = 16          # OwnProcess
$serviceErrorControl = 1   # UserNotified
$loadOrderGroup = $null
$loadOrderGroupDepend = $null
$dependencies = $null

# description?
$params = `
    $serviceName, `
    $displayName, `
    $physicalPath, `
    $serviceType, `
    $serviceErrorControl, `
    $startMode, `
    $interactWithDesktop, `
    $userName, `
    $password, `
    $loadOrderGroup, `
    $loadOrderGroupDepend, `
    $dependencies `

$scope = new-object System.Management.ManagementScope("\\$targetServer\root\cimv2", `
    (new-object System.Management.ConnectionOptions))
"Connecting to $targetServer"
$scope.Connect()
$mgt = new-object System.Management.ManagementClass($scope, `
    (new-object System.Management.ManagementPath("Win32_Service")), `
    (new-object System.Management.ObjectGetOptions))

$op = "service $serviceName ($physicalPath) on $targetServer"   
"Installing $op"
$result = $mgt.InvokeMethod("Create", $params)   
Test-ServiceResult -operation "Install $op" -result $result
"Installed $op"

"Setting $serviceName description to '$description'"
Set-Service -ComputerName $targetServer -Name $serviceName -Description $description
"Service install complete"
}

Geoffrey Hudik

Posted 2011-09-20T14:42:55.380

Reputation: 121