I need to execute a command on Remote box and the task is to wait till the execution finishes and then need to do some other steps. i have written following code using ManagementClass and ManagementOperationObserver (for Asynchrous execution),
it is working fine with win 2003 boxes but when i execute it on 64-bit it is hanging. ALthough my command got executed it is not able to fire the event Completed(observer Delegate). Appreciate any quick help. here goes my code
Class someClass{
private
bool _wait = false;
void somefunction()
{
ManagementScope
sc = null;
ConnectionOptions co = new ConnectionOptions();
// the observer will get a callback when the object we are executing is ready
ManagementOperationObserver observer = new ManagementOperationObserver();
observer.ObjectReady +=
new ObjectReadyEventHandler(observer_ObjectReady);
observer.Completed +=
new CompletedEventHandler(observer_Completed);
co.Authentication =
AuthenticationLevel.Call;
co.Impersonation =
ImpersonationLevel.Impersonate;
co.EnablePrivileges =
true;
// create scope to the remote machine
sc =
new ManagementScope(string.Format("//{0}/ROOT/CIMV2", _machineName), co);
// create a new management class
ManagementClass processClass = new ManagementClass(sc, new ManagementPath("Win32_Process"), null);
object[] methodArgs = { _files[i].Trim(), null, null, 0 };
//Host.UI.WriteLine(string.Format("\t\tRemoteCommand is: {0}", thisCmd));
Host.UI.WriteLine(
string.Format("\t\tRemoteCommand is: {0}", _files[i].Trim()));
// execute the file/command
processClass.InvokeMethod(observer,
"Create", methodArgs);
while
(!_isComplete)
{
// display the time so that hung process can be detected visually
Host.UI.WriteLine(
string.Format("\t\tWaiting... {0}", DateTime.Now.ToLongTimeString()));
// sleep
System.Threading.
Thread.Sleep(_pollInterval);
}
/// <summary>
/// Command complete event.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void observer_Completed(object sender, CompletedEventArgs e)
{
_isComplete =
true;
}
/// <summary>
/// Event handler to process object ready events.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void observer_ObjectReady(object sender, ObjectReadyEventArgs e)
{
_exitCode = e.NewObject.Properties[
"ReturnValue"].Value.ToString();
_processID = e.NewObject.Properties[
"ProcessId"].Value.ToString();
}