2011-02-22 193 views
2

以下代码引发异常。我不知道我在代码中犯了什么错误。请有人帮我弄清楚。我认为这是一些安全权利问题。如果是这样,我怎么能给任何用户或应用程序的安全权限以编程方式访问此Windows服务?ServiceController.start()和ServiceController.stop()抛出异常?

Dim sc As New ServiceController   
sc.ServiceName = "DataLoad" 
If sc.Status = ServiceControllerStatus.Stopped Then  
    sc.Start()  
Else 
    sc.Stop() 
End If 

异常

System.InvalidOperationException: Cannot open DataLoad service on computer '.'. ---> 
System.ComponentModel.Win32Exception: Access is denied --- End of inner exception stack trace --- at 
System.ServiceProcess.ServiceController.GetServiceHandle(Int32 desiredAccess) at 
System.ServiceProcess.ServiceController.Start(String[] args) at 
System.ServiceProcess.ServiceController.Start() at 
WEBSITE.DataLoad.Submit1_ServerClick(Object sender, EventArgs e) in C:\Inetpub\wwwroot\WEBSITE\a\DataLoad.aspx.vb:line 46 

谢谢!

回答

2

可以使用subinacl工具为

SUBINACL /SERVICE \\MachineName\ServiceName /GRANT=[DomainName\]UserName[=Access] 

要specfic为您的情况:

subinacl /service DataLoad /GRANT=YOURDOMAIN\[User in appdomain for WEBSITE]=TO 

TO意味着
T:启动服务
○:一站式服务

[Access]的所有选项均为:

F:完全控制
R:一般性读
宽:通用写
X:通用的execute
L:读取控制
问:查询服务配置
S:查询服务状态
E:列举相关服务
C:服务更改配置
T:启动服务
O:停止服务
,P:暂停/继续服务
I:询问服务
U:服务用户定义的控制命令

参见Method 3 in this kb article

+0

非常感谢您的回答。如果您能解释如何以其他方式(组策略或其他方式),而不使用第三方代码,我将不胜感激。 谢谢! – user536652 2011-02-22 22:33:38

+0

subinacl是Windows资源工具包的一部分。我不认为这是“第三方代码”。 http://www.microsoft.com/downloads/en/details.aspx?FamilyID=e8ba3e56-d8fe-4a91-93cf-ed6985e3927b&displaylang=en – rene 2011-02-23 09:05:00

+0

请任何其他帮助...我仍然在等待... – user536652 2011-02-23 14:35:40

1

我找到了解决该问题通过提供这是机器的机器名目前在ServiceController重载的构造函数中执行该服务,该构造函数需要2(两个)参数,即 public ServiceController(/ 我的服务的名称字符串 /,System.Environment.MachineName/此机器正在执行服务 /)

该解决方案的测试版本是4.5,希望这有助于任何人仍在寻找解决方案。

以下是你需要在代码中做什么:

ServiceController serviceController = new ServiceController("myServiceName", System.Environment.MachineName); 
+0

如果状态没有按时更改,这仍然可以通过启动已启动的服务来解决问题。 – 2013-11-10 13:26:31

-1

如果您已经具有服务用户以LocalSystem(高特权用户)问题不是安全问题。我之前也遇到过这个问题,它的状态vrs再次启动,或者当已经停止()时停止它。

你看服务状态的没有改变的需求,所以即使你编码

//this will start the process but the 
//sc status will take some time to change 
//when that happens and you try to start 
//the already started service it will give you 
//your error 
servicec.start(); 

洙你需要这样做: msdn ServiceController.waitforstatus

Dim sc As New ServiceController 
    sc.ServiceName = "DataLoad" 
If sc.Status = ServiceControllerStatus.Stopped  Then  
    sc.Start()  
// this makes it wait for the status to change 
    // and no it wont slow down anything at all. 
sc.waitforstatus(ServiceControllerStatus.started) 
Else 
    sc.Stop() 
sc.waitforstatus(ServiceControllerStatus.stopped) 
End If 

这将解决您的问题喜欢它做了我的。

0

就我而言,我确定我需要调整服务的安全性,以便在我的服务失败的情况下通过单独的“看门狗”服务重新启动服务。

首先,打开程序mmc.exe,然后添加“安全配置和分析”和安全模板”管理单元。

enter image description here

然后从新建一个空白的安全模板‘安全模板’然后打开“安全配置和分析”,然后选择“打开数据库...”,给它一个名称,然后保存到本地磁盘驱动器的某个方便的地方

然后打开“安全配置和分析”它位于与上一步相同的目录中。当出现“导入模板”窗口时,请在中打开* .inf文件同一个目录。

接下来,右击“安全配置和分析”,然后选择“分析计算机......”下面将出现:

enter image description here

“系统服务”双击,找到并双击点击您的服务,然后点击“在数据库中定义此策略”复选框并点击“编辑安全性”按钮。

这是它变得比什么是链接@JOG描述贴,因为我使用的是Windows 8.1的不同 - 我启用了“启动,停止和暂停”为“互动”和“服务”

enter image description here

仅供参考,我执行了上述按照本指南操作为@JOG建议:https://thommck.wordpress.com/2011/12/02/how-to-allow-non-admins-to-start-and-stop-system-services/