Note that there are some explanatory texts on larger screens.

plurals
  1. POInvocation of Win32_Service UserControlService not working
    text
    copied!<p>I'm using WMI to install/start/stop etc. services on a remote machine. This is working nicely, only the invocation of the <code>UserControlService</code> seems to be a problem.</p> <p>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.</p> <p>Following code:</p> <pre><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(); } } } </code></pre> <p>Throws a <code>System.Management.ManagementException</code> complaining about invalid parameters (with request of 150, which should work). The exception is thrown on <code>theClass.InvokeMethod</code> I'm not sure why this happen, I'm getting the description of the method from:</p> <p><a href="http://msdn.microsoft.com/en-us/library/aa393952(v=vs.85).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/aa393952(v=vs.85).aspx</a></p> <p>Edit: Working version with the corrections by Hans Passant:</p> <pre><code> 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"; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload