English 中文(简体)
WMIC MangementClass RemoteCommand - determining when it is finished? STDOUT?
原标题:
  • 时间:2011-09-21 15:58:39
  •  标签:
  • c#
  • wmi
  • wmic

I m executing a remote CMD line command via WMIC that takes a few seconds to run. I m currently doing Thread.Sleep(4000) before moving on...there MUST be a better way! Is there a variable or method I can use to determine if the command I issued finished / a status byte?

Thanks!

Im using the following code to issue the commands:

ManagementClass processTask = new ManagementClass(@"\" + this.wmiConnection.machineName + @"
ootCIMV2", "Win32_Process", null);
        ManagementBaseObject methodParams = processTask.GetMethodParameters("Create");
        methodParams["CommandLine"] = command;
        methodParams["CurrentDirectory"] = @"C:";

Just need to figure out how to determine when the command finishes :). Thanks!

问题回答

In my understanding, when you write this :

ManagementClass processTask = new ManagementClass(@"\192.168.183.100
ootCIMV2", "Win32_Process", null);
ManagementBaseObject methodParams = processTask.GetMethodParameters("Create");
methodParams["CommandLine"] = "cmd.exe";
methodParams["CurrentDirectory"] = @"C:";

//Execute the method
ManagementBaseObject outParams = processTask.InvokeMethod("Create", methodParams, null);

You are launching the remote process in a synchronous way, so

outParams["returnvalue"]
outParams["processid"]

Will give the return code and processId as explain in How To: Execute a Method, if you want to run it asynchronously you can read this : How To: Call a Method Asynchronously.

There is a similar question posted here: Wait for service.InvokeMethod to finish - WMI, C#

The following documentation How To: Execute a Method and How To: Call a Method Asynchronously describe semi-synchronous and asynchronous execution. What you are doing is semi-synchronous execution.

As far as I can tell, this provides feedback on where WMI executed the command successfully. If it is a long running command such as running an installer, stopping a service, or executing a batch file, WMI will return when the installer launches, the service was told to start stopping or the batch file process is started.

To really wait for the new process to exit, as far as I can tell, you will need to poll to see if the process is running, or query if the service is stopped. In your case, depending on the command, poll the process id.

I am still looking into this myself as well.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签