gpt4 book ai didi

c# - 使用C#ServiceController和模拟启动/停止Windows服务

转载 作者:行者123 更新时间:2023-12-03 11:09:38 25 4
gpt4 key购买 nike

我正在为机器人框架开发远程关键字库。该库在C#.net中运行。

远程库是使用NRobot-Server构建的。这正在处理xml-rpc服务器和其他内容,因此我只能为机器人框架编写关键字。仅供引用,此xml-rpc服务器是多线程的。

基于this answerthis demo,我设法将一些东西放在一起。但是我总是Cannot open <<my service>> service on computer '192.168.0.105'

Cannot open <<my service>> service on computer '192.168.0.105'.
at System.ServiceProcess.ServiceController.GetServiceHandle(Int32 desiredAccess)
at System.ServiceProcess.ServiceController.Stop()
at RKL.KeywordLibrary.KeywordLibraryImpl.ControlService(String host, String username, String password, String name, String action, String domain) in C:\Dev\QueueServiceSystemTestRKL\src\RKL\KeywordLibrary\KeywordLibraryImpl.cs:line 115
at RKL.KeywordLibrary.RklKeywordClass.ControlService(String h
ost, String username, String password, String name, String action) in C:\Dev\RKL\src\RKL\KeywordLibrary\RKLKeywordClass.cs:line 21

由于我应该能够远程控制服务,因此开发环境如下所示:
-------------------     --------------------                              
| OS X | | Win 10 |
| | | |
| robot framework | --> | remote keyword |
| | | library (C#) |
------------------- --------------------
|
|
v
---------------------
| Win Server 2019 |
| |
| service |
| |
---------------------

实际代码如下所示(我正在使用 SimpleImpersonation nuget)
public void ControlService(string host, string username, string password, string name, string action)
{
var credentials = new UserCredentials(username, password);
Impersonation.RunAsUser(credentials, SimpleImpersonation.LogonType.Interactive, () =>
{
ServiceController sc = new ServiceController(name, host);
TimeSpan timeout = new TimeSpan(0,0,30);
switch (action)
{
case "start":
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running, timeout);
break;
case "stop":
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
break;
default:
string msg = String.Format("Unknown action: '{0}'", action);
throw new Exception(msg);
}
});
}

另外要提一下,我已经在两台Windows计算机上都创建了 testuser,并且两个用户都是管理员。

目前,我正在尝试禁用所有Windows安全性,但是我已经死了。只是尝试随机的东西。

有人知道哪里可能出问题吗?任何帮助,将不胜感激!

最佳答案

因此,我找到了有关ServiceControllerPermission class并进行了尝试。而且有效

public void ControlService(string host, string username, string password, string name, string action)
{
var credentials = new UserCredentials(username, password);
Impersonation.RunAsUser(credentials, SimpleImpersonation.LogonType.Interactive, () =>
{
ServiceControllerPermission scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, host, name);
scp.Assert();

ServiceController sc = new ServiceController(name, host);
TimeSpan timeout = new TimeSpan(0,0,30);
switch (action)
{
case "start":
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running, timeout);
break;
case "stop":
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
break;
default:
string msg = String.Format("Unknown action: '{0}'", action);
throw new Exception(msg);
}
});
}

关于c# - 使用C#ServiceController和模拟启动/停止Windows服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55511404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com