/// <summary> /// Starts the process. /// </summary> /// <exception cref="System.InvalidOperationException">Cannot Start Running Process.</exception> /// <exception cref="ProcessHelpers.PsExecCommandException">PSTools command did not successfully complete</exception> public void Start() { if (this.IsProcessRunning) { throw new InvalidOperationException("Cannot Start Running Process."); } /* Command: Use PsExec http://ss64.com/nt/psexec.html * - i : Interactive - Run the program so that it interacts with the desktop on the remote system. * - d : Don’t wait for the application to terminate. * - accepteula: Suppress the display of the license dialog. */ const string Args = @" -accepteula -i -d \\{0}{1} ""{2}"""; using ( var psExecProcess = new Process() { StartInfo = new ProcessStartInfo() { UseShellExecute = false, RedirectStandardOutput = false, RedirectStandardError = true, RedirectStandardInput = true, FileName = this.config.ExecPath, Arguments = string.Format(Args, this.hostname, this.GetCredentialPowershellArgs(), this.exePath) } }) using (var psExec = new SystemProcess(psExecProcess, x => x.Kill())) { psExec.Start(); var standardError = ReadWithTimeout(psExecProcess.StandardError); if (!standardError.Contains(string.Format("started on {0} with process ID", this.hostname))) { throw new PsExecCommandException(string.Format("Failure running PsExec: {0}", standardError)); } this.processId = this.GetPid(standardError); } this.IsProcessRunning = true; }
/// <summary> /// Immediately stops the associated process. /// </summary> /// <exception cref="System.InvalidOperationException">Cannot Terminate Non-Running Process.</exception> /// <exception cref="PsExecCommandException">PSTools command did not successfully complete</exception> public void Kill() { if (!this.IsProcessRunning) { throw new InvalidOperationException("Cannot Terminate Non-Running Process."); } /* Command: Use PsKill http://ss64.com/nt/pskill.html * - accepteula: Suppress the display of the license dialog. */ const string Args = @" -accepteula \\{0} ""{1}"""; using ( var psKillProcess = new Process() { StartInfo = new ProcessStartInfo() { UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = false, RedirectStandardInput = true, FileName = this.config.KillPath, Arguments = string.Format(Args, this.hostname, this.processId) // -t treekill seems to regularly cause PsKill to crash } }) using (var psKill = new SystemProcess(psKillProcess, x => x.Kill())) { psKill.Start(); var standardOutput = ReadWithTimeout(psKillProcess.StandardOutput); if ( !(standardOutput.Contains(string.Format("Process {0} killed", this.processId)) || standardOutput.Contains(string.Format("Process {0} on {1} killed", this.processId, this.hostname)))) { throw new PsExecCommandException(string.Format("Failure running PsExec: {0}", standardOutput)); } } this.IsProcessRunning = false; }