简体   繁体   English

Powershell:Runspace / Pool我如何传递函数和共享变量?

[英]Powershell: Runspace/Pool how do i pass a function and shared variable?

I am trying to create a thread that will use a shared variable (between main session and the thread) plus the give the thread ability to use outside function from the main code 我正在尝试创建一个线程,该线程将使用共享变量(在主会话和线程之间),并赋予线程使用主代码中外部函数的能力

I have managed to pass the function for the thread to use and i have managed to pass read only variable. 我设法通过函数供线程使用,我设法通过只读变量。 my problem is that if i am changing the value of the variable inside the thread and then i try to read it from the main session - i can not see the change in the value, therefore its not shared. 我的问题是,如果我在线程内更改变量的值,然后尝试从主会话读取它-我看不到值的更改,因此无法共享。

How do i do that? 我怎么做? My goal is to have one thread in the end. 我的目标是最终拥有一个线程。

this is my code: 这是我的代码:

$x = [Hashtable]::Synchronized(@{})

$global:yo = "START"

Function ConvertTo-Hex {
    #Write-Output "Function Ran"
    write-host "hi"
    $x.host.ui.WriteVerboseLine("===========")
    write-host $yo
    $yo="TEST"
    write-host $yo
}
#endregion

        $rs = [RunspaceFactory]::CreateRunspace()
        $rs.ApartmentState,$rs.ThreadOptions = "STA","ReUseThread"
        $rs.Open()
        $rs.SessionStateProxy.SetVariable("x",$x)

ls
# create an array and add it to session state
$arrayList = New-Object System.Collections.ArrayList
$arrayList.AddRange(('a','b','c','d','e'))
 $x.host = $host
$sessionstate = [system.management.automation.runspaces.initialsessionstate]::CreateDefault()
$sessionstate.Variables.Add((New-Object System.Management.Automation.Runspaces.SessionStateVariableEntry('arrayList', $arrayList, $null)))
$sessionstate.Variables.Add((New-Object System.Management.Automation.Runspaces.SessionStateVariableEntry('x', $x, $null)))
#$sessionstate.Variables.Add((New-Object System.Management.Automation.Runspaces.SessionStateVariableEntry('yo', $yo, $true)))
$sessionstate.Commands.Add((New-Object System.Management.Automation.Runspaces.SessionStateFunctionEntry -ArgumentList 'ConvertTo-Hex', (Get-Content Function:\ConvertTo-Hex -ErrorAction Stop)))

$runspacepool = [runspacefactory]::CreateRunspacePool(1, 2, $sessionstate, $Host)
$runspacepool.Open()

$ps1 = [powershell]::Create()
$ps1.RunspacePool = $runspacepool

$ps1.AddScript({
    for ($i = 1; $i -le 15; $i++)
    {
        $letter = Get-Random -InputObject (97..122) | % {[char]$_} # a random lowercase letter
        $null = $arrayList.Add($letter)
        start-sleep -s 1
    }
})
$ps1.AddParameter(@($yo))

# on the first thread start a process that adds values to $arrayList every second
$handle1 = $ps1.BeginInvoke()

# now on the second thread, output the value of $arrayList every 1.5 seconds
$ps2 = [powershell]::Create()
$ps2.RunspacePool = $runspacepool

$ps2.AddScript({
     Write-Host "ArrayList contents is "
     foreach ($i in $arrayList)
     {
        Write-Host $i  -NoNewline
        Write-Host " " -NoNewline
     }
     Write-Host ""
     $yo = "BAH"
     ConvertTo-Hex
})
$ps2.AddParameter(@($yo))


1..10 | % {
    $handle2 = $ps2.BeginInvoke()
    if ($handle2.AsyncWaitHandle.WaitOne())
    {
        $ps2.EndInvoke($handle2)
    }
    start-sleep -s 1.5
    write-host "====================" + $yo
}
$global:yo = "test"

您需要在函数内部写入$global

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过引用 Powershell 作业或运行空间传递变量? - How to pass variable by reference to Powershell job or runspace? RunSpacePool如何传递共享变量? - RunSpacePool how do i pass a shared variable? 如何在不同的运行空间中运行功能,PowerShell - How to run function in different runspace, PowerShell 如何从 Powershell Runspace 连接回 Powershell 主线程? - How do I connect from a Powershell Runspace back to the main Powershell thread? 如何将 SwitchParameter 传递给 Powershell 中的 function? - How do I pass a SwitchParameter to a function in Powershell? 不使用运行空间池来管理Powershell运行空间的正确方法是什么? - What is the right way to manage powershell runspace without using runspace pool? 如何通过New-Variable将数组变量注入PowerShell Runspace - How to Inject an Array Variable into a PowerShell Runspace via New-Variable 如何将CMD(%2)中的第二个参数传递到PowerShell变量中 - How do I pass second parameter from CMD (%2) into a PowerShell variable 在Powershell中,如何将变量传递给要添加到数组的命令? - In Powershell, how do I pass a variable to a command to be added to an array? 如何将项目动态添加到 PowerShell ArrayList 并使用运行空间池递归处理它们? - How to dynamically add items to a PowerShell ArrayList and process them recursively using Runspace pool?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM