Advertisement
Guest User

Untitled

a guest
Feb 1st, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. cmd /c C:test.exe
  2.  
  3. private void ExecuteCommand(string command)
  4. {
  5. ConnectionOptions connection = new ConnectionOptions { Username = UserName, Password = Password };
  6. this.wmiScope = new ManagementScope(string.Format(@"\{0}rootCIMV2", IPAddress), connection);
  7.  
  8. try
  9. {
  10. this.wmiScope.Connect();
  11. }
  12. catch (Exception e)
  13. {
  14. var exceptionMessage = string.Format("Management Connect to remote machine {0} failed with the following error {1}", this.dutConfig.IPAddress, e.Message);
  15. throw new Exception(exceptionMessage);
  16. }
  17.  
  18. this.Logger.AddMessage("Start wmi process: {0}", command);
  19. ObjectGetOptions objectGetOptions = new ObjectGetOptions();
  20. ManagementPath managementPath = new ManagementPath("Win32_Process");
  21. using (ManagementClass processClass = new ManagementClass(this.wmiScope, managementPath, objectGetOptions))
  22. {
  23. using (ManagementBaseObject inParams = processClass.GetMethodParameters("Create"))
  24. {
  25. inParams["CommandLine"] = command;
  26. using (ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null))
  27. {
  28. if (outParams != null && (uint)outParams["returnValue"] != 0)
  29. {
  30. throw new Exception(string.Format("Error while starting process {0} creation returned an exit code of {1}", command, outParams["returnValue"]));
  31. }
  32.  
  33. if (outParams != null)
  34. {
  35. this.processId = (uint)outParams["processId"];
  36. this.ExitCode = Convert.ToUInt16(outParams["returnValue"]);
  37. }
  38. }
  39. }
  40. }
  41.  
  42. this.Logger.AddMessage("Start monitoring event for wmi process: {0}, with id: {1}", command, this.processId);
  43. this.EventWatcher();
  44. this.Logger.AddMessage("Wmi Command has finished");
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement