1

I am using the win32-service gem to create a Windows service using Ruby (1.9.3-p429, MRI).

This snippet of code works.

require 'rubygems'
require 'win32/service'

include Win32

SERVICE_NAME = 'myservice'

# Create a new service
Service.create({
  :service_name        => SERVICE_NAME,
  :service_type       => Service::WIN32_OWN_PROCESS,
  :description        => 'A custom service I wrote just for fun',
  :start_type         => Service::AUTO_START,
  :error_control      => Service::ERROR_NORMAL,
  :binary_path_name   => 'c:\Ruby193\bin\ruby.exe -C c:\ c:\myservice.rb',
  :load_order_group   => 'Network',
  #:service_start_name => 'Administrator',
  #:password           => 'adminpasswd',
  :display_name       => SERVICE_NAME
})

Service.start SERVICE_NAME

The problem I have is that the service needs to run with Administrator privileges, but the entity which calls this code snippet runs as the Local System Account, and that is the default privilege.

I can open up up the Services GUI (services.msc) and go in and stop the service, raise the privileges via the "Log On" tab of the service (myservice) properties, and use Administrator/adminpasswd as the user/password. It then runs the service with sufficient privileges.

However, when I try to call Service.create with the :service_start_name and :password set to exactly the same values (by uncommenting the lines in the code snippet) as I used in the Services tab, it doesn't work. This server is an Amazon EC2 server running Windows 2008r2 Datacenter Edition and is not a part of any Windows domain that I know of (because I started it).

What do I need to do differently to get this Windows service running with Administrator privileges?

4

2 回答 2

2

鲍罗丁给了我这个答案的线索。当我回到服务 GUI 重新配置服务时,我注意到虽然我输入了“Administrator”作为用户名,但实际显示在面板中的用户名是“.\Administrator”。牢记鲍罗丁的评论,看来我可以指定“。” 作为域。

所以......实际工作的代码是:

require 'rubygems'
require 'win32/service'

include Win32

SERVICE_NAME = 'myservice'

# Create a new service
Service.create({
  :service_name        => SERVICE_NAME,
  :service_type       => Service::WIN32_OWN_PROCESS,
  :description        => 'A custom service I wrote just for fun',
  :start_type         => Service::AUTO_START,
  :error_control      => Service::ERROR_NORMAL,
  :binary_path_name   => 'c:\Ruby193\bin\ruby.exe -C c:\ c:\myservice.rb',
  :load_order_group   => 'Network',
  :service_start_name => '.\Administrator',
  :password           => 'adminpasswd',
  :display_name       => SERVICE_NAME
})

Service.start SERVICE_NAME
于 2013-07-18T14:52:39.497 回答
2

底层的CreateServiceWindows API 函数需要在字段上有一个帐户域lpServiceStartName,因此您可能需要将该:service_start_name字段设置为'domain\Administrator',其中帐户域通常是计算机名称。

于 2013-07-17T22:33:43.850 回答