C# 检查 windows 服务状态
C# check windows Service status
我正在使用以下代码检查我的 windows 服务状态:
ServiceController sc = new ServiceController("service name", "service location");
在服务位置,我输入了我的服务所在的服务器名称。
在我的测试环境中,我有服务和门户 (IIS),我在同一台服务器上检查我的服务并且工作正常,但在我的生产环境中,该服务与我的门户 IIS 在不同的服务器上。
而且我的代码无法检查状态。我确定这是一个权限问题,但我尝试了很多次以使其工作但没有用。
我的问题是:什么"type of permission"要给什么"user"或"machine name"?请帮忙
您的 IIS 门户是否模拟?似乎是这样。因此,您正在陷入 Kerberos constrained delegation ('two-hop')。你不能 'fix' 这个。您必须请求域管理员为您的 IIS 应用程序启用和配置约束委派。
另一种方法是不在您的 IIS 中模拟。在这种情况下,您需要为您的门户应用程序池授予适当的权限。 SC_MANAGER_CONNECT
. Read more at KB179249: Access to the Service Control Manager 中所需的权限。很明显,app-pool 必须是域帐户,并且 IIS 和目标服务主机应该在同一个域中,或者在具有信任关系的域中。
多亏了所有人,我通过 WMI 使用了另一种方法并且成功了:
ConnectionOptions op = new ConnectionOptions();
op.Username = "";
op.Password = "";
ManagementScope scope = new ManagementScope(SVClocation+@"\root\cimv2", op);
scope.Connect();
ManagementPath path = new ManagementPath("Win32_Service");
ManagementClass services = new ManagementClass(scope, path, null);
foreach (ManagementObject service in services.GetInstances())
{
if (service.GetPropertyValue("Name").ToString().ToLower().Equals("serviceName"))
{
if (service.GetPropertyValue("State").ToString().ToLower().Equals("running"))
{
//do something
}
else
{
//do something
}
}
}
我正在使用以下代码检查我的 windows 服务状态:
ServiceController sc = new ServiceController("service name", "service location");
在服务位置,我输入了我的服务所在的服务器名称。 在我的测试环境中,我有服务和门户 (IIS),我在同一台服务器上检查我的服务并且工作正常,但在我的生产环境中,该服务与我的门户 IIS 在不同的服务器上。
而且我的代码无法检查状态。我确定这是一个权限问题,但我尝试了很多次以使其工作但没有用。
我的问题是:什么"type of permission"要给什么"user"或"machine name"?请帮忙
您的 IIS 门户是否模拟?似乎是这样。因此,您正在陷入 Kerberos constrained delegation ('two-hop')。你不能 'fix' 这个。您必须请求域管理员为您的 IIS 应用程序启用和配置约束委派。
另一种方法是不在您的 IIS 中模拟。在这种情况下,您需要为您的门户应用程序池授予适当的权限。 SC_MANAGER_CONNECT
. Read more at KB179249: Access to the Service Control Manager 中所需的权限。很明显,app-pool 必须是域帐户,并且 IIS 和目标服务主机应该在同一个域中,或者在具有信任关系的域中。
多亏了所有人,我通过 WMI 使用了另一种方法并且成功了:
ConnectionOptions op = new ConnectionOptions();
op.Username = "";
op.Password = "";
ManagementScope scope = new ManagementScope(SVClocation+@"\root\cimv2", op);
scope.Connect();
ManagementPath path = new ManagementPath("Win32_Service");
ManagementClass services = new ManagementClass(scope, path, null);
foreach (ManagementObject service in services.GetInstances())
{
if (service.GetPropertyValue("Name").ToString().ToLower().Equals("serviceName"))
{
if (service.GetPropertyValue("State").ToString().ToLower().Equals("running"))
{
//do something
}
else
{
//do something
}
}
}