I'm trying to execute a PS1 file via a C# application and capture/segment the output based on severity. Error, Debug, Info, etc. This is a Windows Form application. Here's the Run Method, which I call on a button click:
PowerShell m_PSInst = PowerShell.Create();
//Add the script to execute
m_PSInst.AddScript(fileName);
//Attach to the Info Stream
m_PSInst.Streams.Information.DataAdded += (sender, args) =>
{
var psData = (PSDataCollection<InformationRecord>)sender;
var results = psData.ReadAll();
foreach (var result in results)
{
AddInfo(result.ToString());
}
};
//Run Async
Task.Factory.FromAsync(m_PSInst.BeginInvoke(), pResult => m_PSInst.EndInvoke(pResult));
Inside my AddInfo method, I'm checking for Threading access to the UI layer
public void AddInfo(string value)
{
if (InvokeRequired)
{
Invoke(new Action<string>(AddInfo), value);
return;
}
infoTextbox.AppendText(value, Color.White);
allOutputTextbox.AppendText(value, Color.White);
Application.DoEvents();
}
The contents of my PS1 file are as follows:
Write-Information "Test-Before"
Get-WebBinding
Write-Information "Test-After"
What I see is "Test-Before" and that's it. All Output streams seem to not receive anything after the Get-WebBinding Command. I've tried adding in a Out-Default, Out-String and even directing to Out-File. Nothing seems to work.
What's not pictured here is the other DataAdded methods for the other streams including Error, Debug, Verbose, Progress and Warning. Would love to stop kicking myself on this one. :)
I think this is a 32/64 bit thing. Some Cmdlets only run under 64-bit mode, including the
Get-WebBinding
Cmdlet.Try making sure your pshost is compiled to 64 bit.