public object Execute(IDictionary <string, object> arguments) { // string fn = String.Empty; // create the process info.. Process process = new Process(); // set the defaults.. process.StartInfo.CreateNoWindow = true; process.StartInfo.UseShellExecute = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; // set the default timeout.. int timeout = 1000 * 30; // 30 secss IDictionary <string, object> result = new Dictionary <string, object>(); int exitCode = 1; try { Trace.TraceInformation("About to execute script: {0}", _script); // if there are any environment varibles set to false.. process.StartInfo.UseShellExecute = false;//arguments.Count == 0; // take out username and password if they're in the options. foreach (KeyValuePair <string, object> kv in arguments) { if (kv.Key.ToUpper().Equals("USERNAME")) { string domainUser = kv.Value.ToString(); string[] split = domainUser.Split(new char[] { '\\' }); if (split.Length == 1) { process.StartInfo.UserName = split[0]; } else { process.StartInfo.Domain = split[0]; process.StartInfo.UserName = split[1]; } } else if (kv.Key.ToUpper().Equals("PASSWORD")) { if (kv.Value is SecureString) { process.StartInfo.Password = (SecureString)kv.Value; } else if (kv.Value is GuardedString) { process.StartInfo.Password = ((GuardedString)kv.Value).ToSecureString(); } else { throw new ArgumentException("Invalid type for password."); } } else if (kv.Key.ToUpper().Equals("WORKINGDIR")) { process.StartInfo.WorkingDirectory = kv.Value.ToString(); } else if (kv.Key.ToUpper().Equals("TIMEOUT")) { timeout = Int32.Parse(kv.Value.ToString()); } else { if (kv.Value == null) { Trace.TraceWarning("...parameter {0} has null value, skipping it", kv.Key); } else { Trace.TraceInformation("...with parameter {0} set to {1}", kv.Key, kv.Value); process.StartInfo.EnvironmentVariables[kv.Key] = kv.Value.ToString(); } } } // write out the script.. fn = Path.GetTempFileName() + ".cmd"; StreamWriter sw = null; try { sw = new StreamWriter(fn); sw.Write(_script); } finally { sw.Close(); sw.Dispose(); } // set temp file.. process.StartInfo.FileName = fn; // execute script.. process.Start(); string stdout = process.StandardOutput.ReadToEnd(); Trace.TraceInformation("execution started; stdout = {0}", stdout); // this is quite suspicious... // http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx // Use asynchronous read operations on at least one of the streams. AsynchronousReader msr_stderr = new AsynchronousReader(process.StandardError); // Create the thread objects to run the code asynchronously Thread t_stderr = new Thread(msr_stderr.Go); t_stderr.Start(); t_stderr.Join(); // wait for the process to exit.. if (!process.WaitForExit(timeout)) { throw new TimeoutException("Script failed to exit in time!"); } exitCode = process.ExitCode; result.Add("stdout", stdout); result.Add("stderr", msr_stderr.Text); Trace.TraceInformation("execution finished; stderr = {0}", msr_stderr.Text); } catch (Exception e) { Trace.TraceError("Failed to execute script with exception {0}", e.Message); Trace.TraceError("Details: {0}", e); } finally { // close up the process process.Close(); process.Dispose(); } // clean up temp file.. try { File.Delete(fn); } catch (Exception) { } Trace.TraceInformation("exitCode = {0}", exitCode); result.Add("exitCode", exitCode); return(result); }
public object Execute(IDictionary<string, object> arguments) { // string fn = String.Empty; // create the process info.. Process process = new Process(); // set the defaults.. process.StartInfo.CreateNoWindow = true; process.StartInfo.UseShellExecute = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; // set the default timeout.. int timeout = 1000 * 30; // 30 secss IDictionary<string, object> result = new Dictionary<string, object>(); int exitCode = 1; try { Trace.TraceInformation("About to execute script: {0}", _script); // if there are any environment varibles set to false.. process.StartInfo.UseShellExecute = false;//arguments.Count == 0; // take out username and password if they're in the options. foreach (KeyValuePair<string, object> kv in arguments) { if (kv.Key.ToUpper().Equals("USERNAME")) { string domainUser = kv.Value.ToString(); string[] split = domainUser.Split(new char[] { '\\' }); if (split.Length == 1) { process.StartInfo.UserName = split[0]; } else { process.StartInfo.Domain = split[0]; process.StartInfo.UserName = split[1]; } } else if (kv.Key.ToUpper().Equals("PASSWORD")) { if (kv.Value is SecureString) { process.StartInfo.Password = (SecureString)kv.Value; } else if (kv.Value is GuardedString) { process.StartInfo.Password = ((GuardedString)kv.Value).ToSecureString(); } else { throw new ArgumentException("Invalid type for password."); } } else if (kv.Key.ToUpper().Equals("WORKINGDIR")) { process.StartInfo.WorkingDirectory = kv.Value.ToString(); } else if (kv.Key.ToUpper().Equals("TIMEOUT")) { timeout = Int32.Parse(kv.Value.ToString()); } else { if (kv.Value == null) { Trace.TraceWarning("...parameter {0} has null value, skipping it", kv.Key); } else { Trace.TraceInformation("...with parameter {0} set to {1}", kv.Key, kv.Value); process.StartInfo.EnvironmentVariables[kv.Key] = kv.Value.ToString(); } } } // write out the script.. fn = Path.GetTempFileName() + ".cmd"; StreamWriter sw = null; try { sw = new StreamWriter(fn); sw.Write(_script); } finally { sw.Close(); sw.Dispose(); } // set temp file.. process.StartInfo.FileName = fn; // execute script.. process.Start(); string stdout = process.StandardOutput.ReadToEnd(); Trace.TraceInformation("execution started; stdout = {0}", stdout); // this is quite suspicious... // http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx // Use asynchronous read operations on at least one of the streams. AsynchronousReader msr_stderr = new AsynchronousReader(process.StandardError); // Create the thread objects to run the code asynchronously Thread t_stderr = new Thread(msr_stderr.Go); t_stderr.Start(); t_stderr.Join(); // wait for the process to exit.. if (!process.WaitForExit(timeout)) { throw new TimeoutException("Script failed to exit in time!"); } exitCode = process.ExitCode; result.Add("stdout", stdout); result.Add("stderr", msr_stderr.Text); Trace.TraceInformation("execution finished; stderr = {0}", msr_stderr.Text); } catch (Exception e) { Trace.TraceError("Failed to execute script with exception {0}", e.Message); Trace.TraceError("Details: {0}", e); } finally { // close up the process process.Close(); process.Dispose(); } // clean up temp file.. try { File.Delete(fn); } catch (Exception e) { } Trace.TraceInformation("exitCode = {0}", exitCode); result.Add("exitCode", exitCode); return result; }