public void LaunchProcess(string command, string parameters, CommandPassthorugh cpt) { cpass = cpt; System.Management.Automation.Runspaces.Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(); runspace.Open(); PowerShell ps = PowerShell.Create(); // Create a new PowerShell instance ps.Runspace = runspace; // Add the instance to the runspace ps.Commands.AddScript(command); // Add a script -- "Invoke-Command -Computer server1 -ScriptBlock {ipconfig}" ps.Commands.AddStatement().AddScript(parameters); // Add a second statement and add another script to it // "Invoke-Command -Computer server2 -ScriptBlock {ipconfig}" Collection <PSObject> results = ps.Invoke(); runspace.Close(); StringBuilder stringBuilder = new StringBuilder(); foreach (PSObject obj in results) { cpass?.Invoke(obj.ToString()); } }
public void LaunchProcess(string command, string parameters, CommandPassthorugh cpt, CommandPassthorugh ept = null) { cpass = cpt; if (ept == null) { epass = cpt; } else { epass = ept; } process.EnableRaisingEvents = true; process.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(process_OutputDataReceived); process.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler(process_ErrorDataReceived); process.Exited += new System.EventHandler(process_Exited); process.StartInfo.FileName = "CMD.EXE"; process.StartInfo.Arguments = "/C " + command;//parameters; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardError = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.Start(); process.BeginErrorReadLine(); process.BeginOutputReadLine(); //below line is optional if we want a blocking call //process.WaitForExit(); }