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;
}
Powershell.Create
. All output streams are available through the Powershell.Streams property – EnterectomyWrite-Host
is an alias forWrite-Information
. It shouldn't raise any errors. You're using an outdated Powershell version. The solution is to upgrade it to the current version – Enterectomy