Reputation: 79
When running the script in Powershell i'm able to receive the Write-Host output but not in C#.
Here is the code for the output Write-Host "HELLO WORLD TEST."
The application fails when it reaches this line:
Collection<PSObject> results = pipeline.Invoke()
;
I receive this error message:
HostException: A command that prompts the user failed because the host program or the command type does not support user interaction. Try a host program that supports user interaction, such as the Windows PowerShell Console or Windows PowerShell ISE, and remove prompt-related commands from command types that do not support user interaction, such as Windows PowerShell workflows
How can I return the output of Write-Host? Thanks in advance.
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
RunScript(@"C:\TestScript.ps1");
}
}
private string RunScript(string scriptText) {
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
pipeline.Commands.Add("Out-String");
Collection < PSObject > results = pipeline.Invoke();
runspace.Close();
StringBuilder stringBuilder = new StringBuilder();
foreach(PSObject obj in results) {
//do something
}
return Textbox.Text;
}
Upvotes: 2
Views: 4488
Reputation: 140
If you want to keep using the Pipeline class, you can use the Command.MergeMyResults
method. For example, to redirect all type of streams to pipeline output:
private string RunScript(string scriptText) {
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript("Write-Host Test");
pipeline.Commands[pipeline.Commands.Count-1]
.MergeMyResults(PipelineResultTypes.All, PipelineResultTypes.Output)
Collection < PSObject > results = pipeline.Invoke();
runspace.Close();
foreach(PSObject obj in results) {
Console.WriteLine(obj.ToString());
}
}
Upvotes: 0
Reputation: 1664
You can work with PowerShell like this. Create an instance and add listeners for all the Powershell streams that are of interest to you:
private string RunScript(string scriptText)
{
System.Management.Automation.PowerShell powerShellInstance = System.Management.Automation.PowerShell.Create();
powerShellInstance.Streams.Information.DataAdded += InformationHandler;
powerShellInstance.Streams.Verbose.DataAdded += InformationalRecordEventHandler<VerboseRecord>;
powerShellInstance.Streams.Debug.DataAdded += InformationalRecordEventHandler<DebugRecord>;
powerShellInstance.Streams.Warning.DataAdded += InformationalRecordEventHandler<WarningRecord>;
powerShellInstance.AddScript(scriptText);
powerShellInstance.Invoke();
}
static void InformationalRecordEventHandler<T>(object sender, DataAddedEventArgs e)
where T : InformationalRecord
{
var newRecord = ((PSDataCollection<T>)sender)[e.Index];
if (!string.IsNullOrEmpty(newRecord.Message))
{
//STORE your message somewhere
}
}
static void InformationHandler(object sender, DataAddedEventArgs e)
{
var newRecord = ((PSDataCollection<InformationRecord>)sender)[e.Index];
if (newRecord?.MessageData != null)
{
//STORE your message somewhere
}
}
Upvotes: 5