How can I remotely install or uninstall a service on Windows Server 2008 R2?
Solution 1
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"
}
Solution 2
Powershell seems like a likely option. Take a look at the cmdlet reference and the Add-WindowsFeature cmdlet
Comments
-
derFunk over 1 year
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.
-
Harry Johnston over 12 yearsThis sounds like a programming question, perhaps it should be on stackoverflow instead of superuser?
-
Harry Johnston over 12 yearsIs the installer (setup.exe) your own code?
-
derFunk over 12 yearsIts 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 ;)
-
Harry Johnston over 12 yearsFrom 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.
-
derFunk over 12 yearsThe 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?
-
Harry Johnston over 12 yearsWell, it depends on the service. Have you tried using the sc create command from a server-side script?
-
surfasb over 12 yearsPowershell is the way of the future. Even Microsoft has started creating all their diagnostic tools on top of Powershell.
-
derFunk over 12 yearsIts 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).
-
uSlackr over 12 yearsPowershell v2's remoting service runs over an ssh-like connection.