SessionStateProxy Variable with Runspace Pools

一笑奈何 提交于 2019-12-13 02:39:24

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/38102068/sessionstateproxy-variable-with-runspace-pools

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!