简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM