Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- cmd /c C:test.exe
- private void ExecuteCommand(string command)
- {
- ConnectionOptions connection = new ConnectionOptions { Username = UserName, Password = Password };
- this.wmiScope = new ManagementScope(string.Format(@"\{0}rootCIMV2", IPAddress), connection);
- try
- {
- this.wmiScope.Connect();
- }
- catch (Exception e)
- {
- var exceptionMessage = string.Format("Management Connect to remote machine {0} failed with the following error {1}", this.dutConfig.IPAddress, e.Message);
- throw new Exception(exceptionMessage);
- }
- this.Logger.AddMessage("Start wmi process: {0}", command);
- ObjectGetOptions objectGetOptions = new ObjectGetOptions();
- ManagementPath managementPath = new ManagementPath("Win32_Process");
- using (ManagementClass processClass = new ManagementClass(this.wmiScope, managementPath, objectGetOptions))
- {
- using (ManagementBaseObject inParams = processClass.GetMethodParameters("Create"))
- {
- inParams["CommandLine"] = command;
- using (ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null))
- {
- if (outParams != null && (uint)outParams["returnValue"] != 0)
- {
- throw new Exception(string.Format("Error while starting process {0} creation returned an exit code of {1}", command, outParams["returnValue"]));
- }
- if (outParams != null)
- {
- this.processId = (uint)outParams["processId"];
- this.ExitCode = Convert.ToUInt16(outParams["returnValue"]);
- }
- }
- }
- }
- this.Logger.AddMessage("Start monitoring event for wmi process: {0}, with id: {1}", command, this.processId);
- this.EventWatcher();
- this.Logger.AddMessage("Wmi Command has finished");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement