ForEach -parallel,如何 return/save 输出到数组

ForEach -parallel, how to return/save output to an array

也许有人可以帮我解决一个问题。我想加快脚本处理时间并在 PS 核心或工作流程中并行重写它。

目前,我正在努力将输出保存到并行 foreach 循环内的数组中。 在这里,我将浏览每个存储帐户并仅将 blob 保存到 $StrgAccsObj 中。在我想在下一个循环中使用这个数组 @() 但在这一部分之后,里面什么也没有。我的问题是如何将它保存到数组中?

$strgAccs | ForEach-Object -parallel{
    Write-Output "Checking storage $($_.StorageAccountName)"
    if ($_.Kind -eq "BlobStorage"){Write-Host "BlobStorage,- skipping"; $StrgAccsObj += $_; continue;}
    $tables = Get-AzStorageTable -Context $_.Context
    $queues = Get-AzStorageQueue -Context $_.Context
    $shares = Get-AzStorageShare -Context $_.Context
    if ($tables) {Write-Host "Stopping, SA is actual"; continue;}
    if ($queues) {Write-Host "Stopping, SA is actual"; continue;}
    if ($shares) {Write-Host "Stopping, SA is actual"; continue;}
    else {Write-Host "Saving SA to blob check list"; $StrgAccsObj += $_}
}

非常感谢!

您可以将管道的输出分配给一个变量:

$output = $strgAccs | ForEach-Object -parallel{
    Write-Host "Checking storage $($_.StorageAccountName)"
    if ($_.Kind -eq "BlobStorage"){
        Write-Host "BlobStorage,- skipping"

        # Output
        $_

        # Next
        continue
    }

    $tables = Get-AzStorageTable -Context $_.Context
    $queues = Get-AzStorageQueue -Context $_.Context
    $shares = Get-AzStorageShare -Context $_.Context
    if ($tables) {
        Write-Host "Stopping, SA is actual"; continue
    }
    if ($queues) {
        Write-Host "Stopping, SA is actual"; continue
    }
    if ($shares) {
        Write-Host "Stopping, SA is actual"; continue
    }

    Write-Host "Saving SA to blob check list"
    
    # Output
    $_
}

现在 $output 包含本来会添加到 $StrgAccsObj

的值