Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
powershell运行空间:访问全局变量是,还是否?_Powershell_Runspace - Fatal编程技术网

powershell运行空间:访问全局变量是,还是否?

powershell运行空间:访问全局变量是,还是否?,powershell,runspace,Powershell,Runspace,我在本地系统上导入了这个模块。当我在本地系统上也使用运行空间运行脚本块时,该模块中的函数可以在运行空间脚本块中访问 此模块创建全局变量。我测试了这些全局变量是否也可以从运行空间中运行的脚本块访问。它们都由get variable-scope-global-name返回 但是当我试图从脚本块访问这些全局文件时,只有一些文件通过了 如果所有人都不出现,或者他们都会出现,我可以处理这个问题。但是,他们都表现为在场,但只有一些人是有内容的 目标是使该模块在本地系统和运行空间中运行的本地系统这两种环境中都

我在本地系统上导入了这个模块。当我在本地系统上也使用运行空间运行脚本块时,该模块中的函数可以在运行空间脚本块中访问

此模块创建全局变量。我测试了这些全局变量是否也可以从运行空间中运行的脚本块访问。它们都由get variable-scope-global-name返回 但是当我试图从脚本块访问这些全局文件时,只有一些文件通过了

如果所有人都不出现,或者他们都会出现,我可以处理这个问题。但是,他们都表现为在场,但只有一些人是有内容的


目标是使该模块在本地系统和运行空间中运行的本地系统这两种环境中都能工作。

简而言之,您希望在运行空间中看到的所有内容都应该传递给它

如果您的值是
ValueType
,则应通过引用显式传递它们。否则,您应该按原样传递它们(对于尊敬类型,默认为引用)

下面是我使用
PoshRSJob
模块和自定义
SomeFunction
并行处理有限项列表的模板:

Function SomeFunction
{
    Param(
       [String]$SampleParam
    )
    return $SampleParam.ToUpper()
}

$JobScript = [scriptblock]{
    $inQueue = [System.Collections.Concurrent.ConcurrentQueue[string]]$args[0]
    $outBag = [System.Collections.Concurrent.ConcurrentBag[string]]$args[1]
    $currentItem = $null
    while($inQueue.TryDequeue([ref] $currentItem) -eq $true)
    {
        try
        {
            # Add result to OutBag
            $result = SomeFunction -SampleParam $currentItem -EA Stop
            $outBag.Add( $result )
        }
        catch
        {
            # Catch error
            Write-Output $_.Exception.ToString()
        }
    }
}

$inData = [System.Collections.Concurrent.ConcurrentQueue[string]]::new(
    [String[]](@(1..10000) | % { return [guid]::NewGuid().ToString() }) # Sample strings in lower case
    )

$resultData = [System.Collections.Concurrent.ConcurrentBag[string]]::new()

# Wait for queue to empty
$i_cur = $inData.Count
$i_max = $i_cur

# Start jobs
$jobs = @(1..20) | % { Start-RSJob -ScriptBlock $JobScript -ArgumentList @($inData, $resultData) -FunctionsToImport @('SomeFunction') }

# Wait queue to empty
while($i_cur -gt 0)
{
    Write-Progress -Activity 'Doing job' -Status "$($i_cur) left of $($i_max)" -PercentComplete (100 - ($i_cur / $i_max * 100)) 
    Start-Sleep -Seconds 3 # Update frequency
    $i_cur = $inData.Count
}

# Wait jobs to complete
$logs = $jobs | % { Wait-RSJob -Job $_ } | % { Receive-RSJob -Job $_  } 
$jobs | % { Remove-RSJob -Job $_ }
$Global:resultData = $resultData
$Global:logs = $logs

为什么变量已经出现了?当获取variable-scope'global'-name'时,我的模块中的所有5个全局变量都会显示出来,我想不是`$全球:AAR='YAY!';获取变量-作用域'Global'|%{“$($.Name)=$($.Value)”}排序对象-属性名称|转换为Json-深度20 |输出文件-文件路径S:\OutVarsOutsideJob.Json.txt'-确认:$false-Force;Start Job-Name TestSync-ScriptBlock{Get Variable-Scope'Global'|%{“$($.Name)=$($.Value)”}}排序对象-属性名称| ConvertTo Json-Depth 20 | Out File-FilePath'S:\OutVarsInsideJob.Json.txt'-Confirm:$false-Force}| Out Null;获取作业-名称TestSync |等待作业|删除作业