[英]Invocation of Win32_Service UserControlService not working
I'm using WMI to install/start/stop etc. services on a remote machine. 我正在使用WMI在远程计算机上安装/启动/停止等服务。 This is working nicely, only the invocation of the
UserControlService
seems to be a problem. 这很好用,只有
UserControlService
的调用似乎是个问题。
I know that it would also be possible to impersonate a user and then use the ServiceController class, but as I've already got all other methods I would rather keep the WMI code and get my method to send service control requests working. 我知道也可以冒充用户然后使用ServiceController类,但是因为我已经有了所有其他方法,所以我宁愿保留WMI代码并让我的方法发送服务控制请求。
Following code: 以下代码:
public static string SendServiceControlRequest(string remoteHost, string serviceName, string username,
string password, int request)
{
ConnectionOptions theConnection = new ConnectionOptions();
theConnection.Username = username;
theConnection.Password = password;
ManagementScope theScope = new ManagementScope(string.Format("\\\\{0}\\root\\cimv2", remoteHost), theConnection);
using (ManagementObject theClass = new ManagementObject(theScope, new ManagementPath("Win32_Service"),
new ObjectGetOptions())) // causes an ArgumentOutOfRangeException (Parametername: path)
{
using (ManagementBaseObject inParams = theClass.GetMethodParameters("UserControlService"))
{
inParams["ControlCode"] = (Byte)request;
ManagementBaseObject outParams = theClass.InvokeMethod("UserControlService", inParams, null);
return outParams["ReturnValue"].ToString();
}
}
}
Throws a System.Management.ManagementException
complaining about invalid parameters (with request of 150, which should work). 引发
System.Management.ManagementException
抱怨无效参数(请求150,这应该工作)。 The exception is thrown on theClass.InvokeMethod
I'm not sure why this happen, I'm getting the description of the method from: 在
theClass.InvokeMethod
上抛出异常我不知道为什么会发生这种情况,我从以下方法获得方法的描述:
http://msdn.microsoft.com/en-us/library/aa393952(v=vs.85).aspx http://msdn.microsoft.com/en-us/library/aa393952(v=vs.85).aspx
Edit: Working version with the corrections by Hans Passant: 编辑:Hans Passant更正的工作版本:
public static bool SendServiceControlRequest(string remoteHost, string serviceName, string username,
string password, int request)
{
ConnectionOptions theConnection = new ConnectionOptions();
theConnection.Username = username;
theConnection.Password = password;
ManagementScope theScope = new ManagementScope(string.Format("\\\\{0}\\root\\cimv2", remoteHost),
theConnection);
string servicePath = string.Format("Win32_Service.Name='{0}'", serviceName);
ManagementPath path = new ManagementPath(servicePath);
using (ManagementObject theClass = new ManagementObject(theScope, path,
new ObjectGetOptions()))
{
using (ManagementBaseObject inParams = theClass.GetMethodParameters("UserControlService"))
{
inParams["ControlCode"] = (Byte)request;
ManagementBaseObject outParams = theClass.InvokeMethod("UserControlService", inParams, null);
return outParams["ReturnValue"].ToString() == "0";
}
}
}
Instead of 代替
inParams["ControlCode"] = (Byte)request;
try 尝试
inParams.SetPropertyValue("ControlCode", (Byte)request);
EDIT 编辑
Untested, but this seems to be what you might be looking for: 未经测试,但这似乎是您可能正在寻找的:
Instead of 代替
using (ManagementBaseObject inParams = theClass.GetMethodParameters("UserControlService"))
{
inParams["ControlCode"] = (Byte)request;
ManagementBaseObject outParams = theClass.InvokeMethod("UserControlService", inParams, null);
return outParams["ReturnValue"].ToString();
}
Try utilizing the ManagementClass instances (from http://social.msdn.microsoft.com/Forums/vstudio/en-US/1dbe4995-ce73-4f01-8d9a-6cf1650bce8a/wmi-c-managementclassinvokemethod-failure ): 尝试使用ManagementClass实例(来自http://social.msdn.microsoft.com/Forums/vstudio/en-US/1dbe4995-ce73-4f01-8d9a-6cf1650bce8a/wmi-c-managementclassinvokemethod-failure ):
foreach (var instance in theClass.GetInstances())
{
using (ManagementBaseObject inParams = instance.GetMethodParameters("UserControlService"))
{
inParams["ControlCode"] = (Byte)request;
ManagementBaseObject outParams = instance.InvokeMethod("UserControlService", inParams, null);
return outParams["ReturnValue"].ToString();
}
}
Yes, there's a bug in your code. 是的,您的代码中存在错误。 You correctly added a serviceName argument to your method but you forgot to actually use it.
您正确地向您的方法添加了serviceName参数,但您忘记了实际使用它。 That's important, you really do have to be specific about what particular service you send this control code to.
这一点很重要,您确实需要具体说明您将此控制代码发送给哪些特定服务。
Make it look like this instead: 让它看起来像这样:
var path = string.Format("Win32_Service.Name='{0}'", serviceName);
var thePath = new ManagementPath(path);
var theClass = new ManagementObject(theScope, thePath, null);
// etc...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.