|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Start Remote Process and track the exit codeI am trying to execute an application on a remote system. After checking several of the website I was managed to write a C# application do the same. But I am not able to track the Exit Code of the application. The return value only tells me if the application started successfully. Is their a way to capture the final exit code of the application that is started remotely? using Invoke Method InvokeMethodOptions MethodOptions = new InvokeMethodOptions(null, System.TimeSpan.MaxValue); And ManagementEventWatcher for completion of the applicaiton. Thanks, Suresh Suresh,
Can you show some code how you are starting the process in the first place? -- Show quoteHide quote- Nicholas Paldino [.NET/C# MVP] - mvp@spam.guard.caspershouse.com "Suresh Nagarajan" <SureshNagara***@discussions.microsoft.com> wrote in message news:C26FB70A-C6C6-4EED-B7D1-B085E00295E5@microsoft.com... > Hello, > I am trying to execute an application on a remote system. After checking > several of the website I was managed to write a C# application do the > same. > > But I am not able to track the Exit Code of the application. The return > value only tells me if the application started successfully. > > Is their a way to capture the final exit code of the application that is > started remotely? > > using Invoke Method > InvokeMethodOptions MethodOptions = new InvokeMethodOptions(null, > System.TimeSpan.MaxValue); > > And > > ManagementEventWatcher for completion of the applicaiton. > > > > > Thanks, > Suresh > Sure. Thanks for your help.
private void Form1_Load(object sender, EventArgs e) { ConnectionOptions co = new ConnectionOptions(); co.Impersonation = ImpersonationLevel.Impersonate; co.EnablePrivileges = true; co.Username = "User"; co.Password = "password"; ManagementScope myscope = new ManagementScope(@"\\Computername\ROOT\CIMV2", co); myscope.Connect(); ManagementClass myclass = new ManagementClass(myscope, new ManagementPath("Win32_Process"), new ObjectGetOptions(null, TimeSpan.MaxValue, true)); ManagementBaseObject inparams = myclass.GetMethodParameters("Create"); inparams["CommandLine"] = @"calc.exe"; InvokeMethodOptions MethodOptions = new InvokeMethodOptions(null, System.TimeSpan.MaxValue); ManagementBaseObject outparams = myclass.InvokeMethod("Create", inparams, MethodOptions); string ProcID = outparams["ProcessID"].ToString(); string retval = outparams["ReturnValue"].ToString(); WqlEventQuery wQuery = new WqlEventQuery("Select * From __InstanceDeletionEvent Within 1 Where TargetInstance ISA 'Win32_Process'"); ManagementEventWatcher wWatcher = new ManagementEventWatcher(myscope, wQuery); int i = 1; while (i == 1) { ManagementBaseObject MBOobj = wWatcher.WaitForNextEvent(); if (((ManagementBaseObject)MBOobj["TargetInstance"])["ProcessID"].ToString() == ProcID) { //MessageBox.Show(retval+ " - " + ((ManagementBaseObject)MBOobj["TargetInstance"])["Name"].ToString() + " - " + ((ManagementBaseObject)MBOobj["TargetInstance"])["ExecutablePath"].ToString()); ; i = 5; } } wWatcher.Stop(); } Show quoteHide quote "Nicholas Paldino [.NET/C# MVP]" wrote: > Suresh, > > Can you show some code how you are starting the process in the first > place? > > > -- > - Nicholas Paldino [.NET/C# MVP] > - mvp@spam.guard.caspershouse.com > > "Suresh Nagarajan" <SureshNagara***@discussions.microsoft.com> wrote in > message news:C26FB70A-C6C6-4EED-B7D1-B085E00295E5@microsoft.com... > > Hello, > > I am trying to execute an application on a remote system. After checking > > several of the website I was managed to write a C# application do the > > same. > > > > But I am not able to track the Exit Code of the application. The return > > value only tells me if the application started successfully. > > > > Is their a way to capture the final exit code of the application that is > > started remotely? > > > > using Invoke Method > > InvokeMethodOptions MethodOptions = new InvokeMethodOptions(null, > > System.TimeSpan.MaxValue); > > > > And > > > > ManagementEventWatcher for completion of the applicaiton. > > > > > > > > > > Thanks, > > Suresh > > > > >
Show quote
Hide quote
"Suresh Nagarajan" <SureshNagara***@discussions.microsoft.com> wrote in message You can't get the "exit code" on anything less than Vista and Longhorn,news:9AC3604A-E094-4556-AC40-84063B541245@microsoft.com... > Sure. Thanks for your help. > > private void Form1_Load(object sender, EventArgs e) > { > > ConnectionOptions co = new ConnectionOptions(); > > co.Impersonation = ImpersonationLevel.Impersonate; > co.EnablePrivileges = true; > > co.Username = "User"; > co.Password = "password"; > > ManagementScope myscope = new > ManagementScope(@"\\Computername\ROOT\CIMV2", co); > myscope.Connect(); > > ManagementClass myclass = new ManagementClass(myscope, new > ManagementPath("Win32_Process"), new ObjectGetOptions(null, > TimeSpan.MaxValue, true)); > ManagementBaseObject inparams = > myclass.GetMethodParameters("Create"); > inparams["CommandLine"] = @"calc.exe"; > > InvokeMethodOptions MethodOptions = new > InvokeMethodOptions(null, System.TimeSpan.MaxValue); > ManagementBaseObject outparams = myclass.InvokeMethod("Create", > inparams, MethodOptions); > > string ProcID = outparams["ProcessID"].ToString(); > string retval = outparams["ReturnValue"].ToString(); > > WqlEventQuery wQuery = new WqlEventQuery("Select * From > __InstanceDeletionEvent Within 1 Where TargetInstance ISA 'Win32_Process'"); > > ManagementEventWatcher wWatcher = new > ManagementEventWatcher(myscope, wQuery); > > int i = 1; > > while (i == 1) > { > ManagementBaseObject MBOobj = wWatcher.WaitForNextEvent(); > > if > (((ManagementBaseObject)MBOobj["TargetInstance"])["ProcessID"].ToString() == > ProcID) > { > > //MessageBox.Show(retval+ " - " + > ((ManagementBaseObject)MBOobj["TargetInstance"])["Name"].ToString() + " - " + > ((ManagementBaseObject)MBOobj["TargetInstance"])["ExecutablePath"].ToString()); ; > i = 5; > > } > > } > > wWatcher.Stop(); > > } > > "Nicholas Paldino [.NET/C# MVP]" wrote: > >> Suresh, >> >> Can you show some code how you are starting the process in the first >> place? >> >> >> -- >> - Nicholas Paldino [.NET/C# MVP] >> - mvp@spam.guard.caspershouse.com >> >> "Suresh Nagarajan" <SureshNagara***@discussions.microsoft.com> wrote in >> message news:C26FB70A-C6C6-4EED-B7D1-B085E00295E5@microsoft.com... >> > Hello, >> > I am trying to execute an application on a remote system. After checking >> > several of the website I was managed to write a C# application do the >> > same. >> > >> > But I am not able to track the Exit Code of the application. The return >> > value only tells me if the application started successfully. >> > >> > Is their a way to capture the final exit code of the application that is >> > started remotely? >> > >> > using Invoke Method >> > InvokeMethodOptions MethodOptions = new InvokeMethodOptions(null, >> > System.TimeSpan.MaxValue); >> > >> > And >> > >> > ManagementEventWatcher for completion of the applicaiton. >> > >> > >> > >> > >> > Thanks, >> > Suresh >> > >> >> >> So the following only works on Vista, on XP you get the exit event but without the ExitStatus property. ...... WqlEventQuery q = new WqlEventQuery( "Win32_ProcessStopTrace"); using(ManagementEventWatcher w = new ManagementEventWatcher(q)){ w.EventArrived += new EventArrivedEventHandler(ProcessStoptEventArrived); w.Start(); Console.ReadLine(); // block main thread for test purposes w.Stop(); } } static void ProcessStoptEventArrived(object sender, EventArrivedEventArgs e) { Console.WriteLine("Process : {0}, stopped with ExitStatus :{1}, Console.WriteLine("Process: {0}, Stopped with Code: {1}", (int)(uint)e.NewEvent.Properties["ProcessId"].Value, (int)(uint)e.NewEvent.Properties["ExitStatus"].Value); } .... Willy. Hi Willy,
Thanks for your response. I came across .NET Remoting. without looking deep into it, wanted to know if this is something that I could use to get the same results. Please forgive my ignorance. I am a newbie to C#. Thanks, Suresh Show quoteHide quote "Willy Denoyette [MVP]" wrote: > "Suresh Nagarajan" <SureshNagara***@discussions.microsoft.com> wrote in message > news:9AC3604A-E094-4556-AC40-84063B541245@microsoft.com... > > Sure. Thanks for your help. > > > > private void Form1_Load(object sender, EventArgs e) > > { > > > > ConnectionOptions co = new ConnectionOptions(); > > > > co.Impersonation = ImpersonationLevel.Impersonate; > > co.EnablePrivileges = true; > > > > co.Username = "User"; > > co.Password = "password"; > > > > ManagementScope myscope = new > > ManagementScope(@"\\Computername\ROOT\CIMV2", co); > > myscope.Connect(); > > > > ManagementClass myclass = new ManagementClass(myscope, new > > ManagementPath("Win32_Process"), new ObjectGetOptions(null, > > TimeSpan.MaxValue, true)); > > ManagementBaseObject inparams = > > myclass.GetMethodParameters("Create"); > > inparams["CommandLine"] = @"calc.exe"; > > > > InvokeMethodOptions MethodOptions = new > > InvokeMethodOptions(null, System.TimeSpan.MaxValue); > > ManagementBaseObject outparams = myclass.InvokeMethod("Create", > > inparams, MethodOptions); > > > > string ProcID = outparams["ProcessID"].ToString(); > > string retval = outparams["ReturnValue"].ToString(); > > > > WqlEventQuery wQuery = new WqlEventQuery("Select * From > > __InstanceDeletionEvent Within 1 Where TargetInstance ISA 'Win32_Process'"); > > > > ManagementEventWatcher wWatcher = new > > ManagementEventWatcher(myscope, wQuery); > > > > int i = 1; > > > > while (i == 1) > > { > > ManagementBaseObject MBOobj = wWatcher.WaitForNextEvent(); > > > > if > > (((ManagementBaseObject)MBOobj["TargetInstance"])["ProcessID"].ToString() == > > ProcID) > > { > > > > //MessageBox.Show(retval+ " - " + > > ((ManagementBaseObject)MBOobj["TargetInstance"])["Name"].ToString() + " - " + > > ((ManagementBaseObject)MBOobj["TargetInstance"])["ExecutablePath"].ToString()); ; > > i = 5; > > > > } > > > > } > > > > wWatcher.Stop(); > > > > } > > > > "Nicholas Paldino [.NET/C# MVP]" wrote: > > > >> Suresh, > >> > >> Can you show some code how you are starting the process in the first > >> place? > >> > >> > >> -- > >> - Nicholas Paldino [.NET/C# MVP] > >> - mvp@spam.guard.caspershouse.com > >> > >> "Suresh Nagarajan" <SureshNagara***@discussions.microsoft.com> wrote in > >> message news:C26FB70A-C6C6-4EED-B7D1-B085E00295E5@microsoft.com... > >> > Hello, > >> > I am trying to execute an application on a remote system. After checking > >> > several of the website I was managed to write a C# application do the > >> > same. > >> > > >> > But I am not able to track the Exit Code of the application. The return > >> > value only tells me if the application started successfully. > >> > > >> > Is their a way to capture the final exit code of the application that is > >> > started remotely? > >> > > >> > using Invoke Method > >> > InvokeMethodOptions MethodOptions = new InvokeMethodOptions(null, > >> > System.TimeSpan.MaxValue); > >> > > >> > And > >> > > >> > ManagementEventWatcher for completion of the applicaiton. > >> > > >> > > >> > > >> > > >> > Thanks, > >> > Suresh > >> > > >> > >> > >> > > > You can't get the "exit code" on anything less than Vista and Longhorn, > So the following only works on Vista, on XP you get the exit event but without the > ExitStatus property. > > ...... > WqlEventQuery q = new WqlEventQuery( "Win32_ProcessStopTrace"); > using(ManagementEventWatcher w = new ManagementEventWatcher(q)){ > w.EventArrived += new EventArrivedEventHandler(ProcessStoptEventArrived); > w.Start(); > Console.ReadLine(); // block main thread for test purposes > w.Stop(); > } > } > static void ProcessStoptEventArrived(object sender, EventArrivedEventArgs e) { > > Console.WriteLine("Process : {0}, stopped with ExitStatus :{1}, > Console.WriteLine("Process: {0}, Stopped with Code: {1}", > (int)(uint)e.NewEvent.Properties["ProcessId"].Value, > (int)(uint)e.NewEvent.Properties["ExitStatus"].Value); > > } > .... > > Willy. > >
Best Performance File Compare: MD5/SHA1 or Byte-by-Byte Checking?
Dynamic Code Adding compression Serialization and ISite When in transaction scope how do I run a query outside transaction? Number formatting with leading spaces Error: Index is outside the bounds of the array how to see if a drive is ready on Framework ver 1.1 C# with Postgre DB How to determine if an object is numeric? |
|||||||||||||||||||||||