I know, that with the Parameter $using:foo
I can use a Variable from a different runspace while running ForEach-Object -Parallel
in Powershell 7 and up.
But how can I add the result back to a Variable? The common parameters +=
and $using:
will not work.
For instance:
$AllSubs = Get-AzSubscription
$Date = (Get-Date).AddDays(-2).ToString("yyyy-MM-dd")
$Costs = @()
$AllSubs | Sort-Object -Property Name | ForEach-Object -Parallel {
Set-AzContext $_.Name | Out-Null
Write-Output "Fetching Costs from '$($_.Name)' ..."
$using:Costs += Get-AzConsumptionUsageDetail -StartDate $using:Date -EndDate $using:Date -IncludeAdditionalProperties -IncludeMeterDetails -ErrorAction SilentlyContinue
}
Output:
The assignment expression is not valid. The input to an assignment operator must be an object that is
| able to accept assignments, such as a variable or a property.