Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SessionStateProxy Variable with Runspace Pools

I wanted to use a Runspace Pool in PowerShell to perform Background actions. But I need to access the WPF Window Variable from the Main Thread.

Normal Runspaces have the option:

$runspace.SessionStateProxy.SetVariable('xamGUI',$xamGUI)

But how do I do the same with a RunspacePool?

like image 534
MicroScripter Avatar asked Oct 25 '25 01:10

MicroScripter


1 Answers

It is a little more involved to add a variable to a runspacepool, but still definitely doable. You will need to create an InitialSessionState object and then create a SessionStateVariableEntry object that contains the variable that you want to add to the runspacepool.

[int]$Test = 123498765
#Create the sessionstate variable entry
$Variable = New-object System.Management.Automation.Runspaces.SessionStateVariableEntry -ArgumentList 'Test',$Test,$Null
$InitialSessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()

#Add the variable to the sessionstate
$InitialSessionState.Variables.Add($Variable)

#Create the runspacepool using the defined sessionstate variable
$RunspacePool = [runspacefactory]::CreateRunspacePool(1,$Throttle,$InitialSessionState,$Host)
like image 51
boeprox Avatar answered Oct 27 '25 16:10

boeprox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!