public System.Collections.ObjectModel.Collection <System.Management.Automation.PSObject> RunCommand(CmdletParameterSetBase paramSet, out System.Collections.ObjectModel.Collection <object> errors) { errors = new System.Collections.ObjectModel.Collection <object>(); System.Collections.ObjectModel.Collection <System.Management.Automation.PSObject> results; using (System.Management.Automation.Runspaces.Pipeline pipeline = runspace.CreatePipeline()) { System.Management.Automation.Runspaces.Command cmd = new System.Management.Automation.Runspaces.Command(paramSet.Cmdlet); if (paramSet.Parameters != null) { foreach (CmdletParameterSwitchValuePair parameter in paramSet.Parameters) { if (parameter.Value != null && parameter.Value.IsSet) { cmd.Parameters.Add(parameter.ParameterSwitch, parameter.Value.ValueObject); } } pipeline.Commands.Add(cmd); } results = pipeline.Invoke(); errors = pipeline.Error.ReadToEnd(); //try //{ // results = pipeline.Invoke(); // return results; //} //catch (Exception Ex) //{ // errors = pipeline.Error.ReadToEnd(); //} //return null; } return(results); }
public System.Collections.ObjectModel.Collection <System.Management.Automation.PSObject> RunCommand(CmdletParameterSetBase paramSet, out System.Collections.ObjectModel.Collection <object> errors) { errors = new System.Collections.ObjectModel.Collection <object>(); System.Collections.ObjectModel.Collection <System.Management.Automation.PSObject> results = null; System.Text.StringBuilder cmdString = new System.Text.StringBuilder(paramSet.Cmdlet); System.Text.StringBuilder parameterValueClearScriptBuilder = new System.Text.StringBuilder(); foreach (CmdletParameterSwitchValuePair parameter in paramSet.Parameters) { if (parameter.Value != null && parameter.Value.IsSet) { if (parameter.Value is CmdletSwitchParameter) { cmdString.AppendFormat(" -{0}", parameter.ParameterSwitch); } else { string parameterSting = System.String.Empty, parameterGuidString; switch (parameter.Value.ParameterValueType) { case ParameterValueTypeEnum.String: parameterSting = (string)parameter.Value.ValueObject; break; case ParameterValueTypeEnum.Object: parameterGuidString = System.Guid.NewGuid().ToString("N"); parameterSting = System.String.Format("${0}", parameterGuidString); runspace.SessionStateProxy.SetVariable(parameterGuidString, parameter.Value.ValueObject); parameterValueClearScriptBuilder.AppendLine(System.String.Format("{0} = $NULL", parameterSting)); break; default: System.Diagnostics.Debug.Assert(false, "Parameter object is not set properly. This error should never be seen."); break; } cmdString.AppendFormat(" -{0} {1}", parameter.ParameterSwitch, parameterSting); } } try { using (System.Management.Automation.Runspaces.Pipeline pipeline = runspace.CreatePipeline(cmdString.ToString())) { results = pipeline.Invoke(); errors = pipeline.Error.ReadToEnd(); } } finally { using (System.Management.Automation.Runspaces.Pipeline pipeline = runspace.CreatePipeline(parameterValueClearScriptBuilder.ToString())) { pipeline.Invoke(); } } } return(results); }
static void Main() { System.Management.Automation.Runspaces.Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(); runspace.Open(); System.Management.Automation.Runspaces.Pipeline pipeline = runspace.CreatePipeline(); pipeline.Commands.AddScript("c:\\temp\\something.ps1"); System.Collections.ObjectModel.Collection <System.Management.Automation.PSObject> results = pipeline.Invoke(); runspace.Close(); System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(); foreach (System.Management.Automation.PSObject obj in results) { stringBuilder.AppendLine(obj.ToString()); } System.Console.WriteLine(stringBuilder.ToString()); }