I want to run a Foreach-Object in parallel with the invoke-command passed having a scriptblock that references a function.
$Workload += Query-SqlData -SqlServer $SqlServer -Database master -Query $Tsql | Select Instance, Database
$Workload | ForEach-Object -Parallel {
$ComputerName = $($_.Instance).Split('\')[0]
$SqlServer = $($_.Instance)
$Database = $($_.Database)
Invoke-Command -ComputerName $ComputerName -AsJob -JobName $Database -ScriptBlock ${Function:Start-UpdateStatistics} `
-ArgumentList $SqlServer ,$Database
}
But it fails with:
Cannot validate argument on parameter 'ScriptBlock'. The argument is null
When I run the Invoke-Command directly, it runs as expected.
Invoke-Command -ComputerName $ComputerName -AsJob -JobName $Database -ScriptBlock ${Function:Start-UpdateStatistics} `
-ArgumentList $SqlServer ,$Database
How can i get this to work in a Foreach-Object parallel ?