Parallel start functions
Is there a way to run parallel programmed functions in PowerShell ?
Something like:
Function BuildParallel($configuration)
{
$buildJob = {
param($configuration)
Write-Host "Building with configuration $configuration."
RunBuilder $configuration;
}
$unitJob = {
param()
Write-Host "Running unit."
RunUnitTests;
}
Start-Job $buildJob -ArgumentList $configuration
Start-Job $unitJob
While (Get-Job -State "Running")
{
Start-Sleep 1
}
Get-Job | Receive-Job
Get-Job | Remove-Job
}
Doesn't work because it complains that it doesn't recognize "RunUnitTests" and "RunBuilder" which are functions declared in the same script file. Apparently, this is because the script block is a new context and knows nothing about scripts declared in the same file.
I could try using -InitializationScript in Start-Job, but both RunUnitTests and RunBuilder call more functions declared in the same file or referencing other files, so ...
I'm sure there is a way to do this as it is just modular programming (functions, subroutines and all that).
source to share
You can have the functions in a separate file and import them into the current context where needed using a point source. I do this in my Powershell profile, so some of my custom functions are available.
$items = Get-ChildItem "$PSprofilePath\functions"
$items | ForEach-Object {
. $_.FullName
}
If you want to import a single file, it will be simple:
. C:\some\path\RunUnitTests.ps1
source to share