Custom Host

Topics: Developer Forum
Apr 26, 2010 at 2:39 PM

I have been trying to write a lite custom host that needs to communcate with cmdlets in a singlerunspace from PSHostRawUserInterface object.  I thought if I could access the runspace session state I would be able to get/set PSVariables.  If that is not possible I also noticed that cmdlets can communicate to/from the host by using the PrivateData property.  There is precious little documentation on PrivateData.  Does any body have any ideas or suggestions on how this can be done?  You would think this would be simple.

Apr 26, 2010 at 3:10 PM

Each Runspace you create has a SessionStateProxy object on which there are two methods: GetVariable() and SetVariable().  Those should do the trick for you.

Apr 26, 2010 at 4:12 PM

Actually I tried this. I get a PSInvalidOperationException: A pipeline is already executing. Concurrent SessionStateProxy method call is not allowed.  Below is a real basic example.  I pass the runspace object as a Runspace property in my PSHostUserInterface class. The exception is thrown on the GetVariable method. 

public override void WriteLine(string value)

<font size="2">


</font>  console.WriteLine("Home: " + psHome);<font size="2"> </font><font size="2">




<font size="2">


</font>tring psHome = (string)runspace.SessionStateProxy.GetVariable("PSHOME");<font size="2"> </font>

Apr 26, 2010 at 4:14 PM

Let me try again.  Formatting issue.

public override void WriteLine(string value)


  string psHome = (string)runspace.SessionStateProxy.GetVariable("PSHOME");

  Console.WriteLine("Home: " + psHome);


Apr 27, 2010 at 4:28 AM

Try getting/setting the variables before you start executing commands/script within the runspace.

Apr 27, 2010 at 1:42 PM

Unfortunately, I can't do that.  The variable can change within the runspace.  That is why I thought the host's PrivateData property could be used.  For example a cmdlet or script can set a property like  $host.privatedata.ErrorForegroundColor.  The problem is I can't figure out how to implement it.  The documentation in the SDK regarding this property is not clear.