The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

EricPope (3) [Avatar] Offline
#1
Hi Folks,
I'm trying out the PowerShell hosting example application from MSDN:

"Creating a Console Application that Implements PSHostRawUserInterface"
http://msdn.microsoft.com/en-us/library/ms714612.aspx

I copied the code into my C# console application and it compiled and ran just fine. However, I had a problem when I tried to use the built-in debugging features of PowerShell. Here's an example:

PSConsoleSample: Set-PSDebug -trace 2

PSConsoleSample: 1+1
2

In the PowerShell.exe host you would see a debug message between the "1+1" and the "2" that says "Debug: 1+ 1+1."

So, my question is, what changes do I need to make to the msdn example so that it will output the debug trace messages to the console?

Thanks,
Eric
EricPope (3) [Avatar] Offline
#2
Re: Getting Debug Messages In my Custom Console Host Application
Here's the solution:
I figured out that the debug setting only applies to commands that have "add to history" set to true. So, instead of creating the pipleines as they do in the msdn example, like this:

currentPipeline = myRunSpace.CreatePipeline();
currentPipeline.Commands.AddScript(cmd);

... you should create them like this:

currentPipeline = myRunSpace.CreatePipeline(cmd , true);

The "true" in the CreatePipeline method sets "add to history" to true. If you use an overload without the boolean or set it to false then your host will not recieve writedebug callbacks for those commands.

-EP