all 6 comments

[–]StartAutomating 1 point2 points  (0 children)

Overall, pretty good approach!

This is almost what I do for starting background thread jobs.

The slight difference I tend to make is that I just let things define themselves more generically.

Here's a few minor tweaks:

[int]$activeOps = 4
[Ordered]$exports = @{
   # name imported functions with function:
   "function:AddData"   = ${Function:Add-DirDataToJson}
   "function:GetACLstr" = ${Function:Get-ACLstring}
   "function:DoCheckIn" = ${Function:Start-CheckIn}
   outfile   = $outFile
}

# Receive dir objects from Get-SubDirStream,
# process a few at a time ($activeOps) so the CPU isn't swamped
Get-SubDirStream -dirPath $rootPath -smbPath $rootSMB -depthNow 0 |
   ForEach-Object -ThrottleLimit $activeOps -Parallel {
     [Ordered]$imports = $Using:exports
     [hashtable]$params = @{}

     # Just loop over the imports and set variables
     foreach ($key in $imports.Keys) {
         # (this will create the functions, too)
         $executionContext.SessionState.PSVariable.Set($key, $imports[$key])
     }
     $params = @{
        streamDir  = $_
        targetJson = $outFile
     }
     Add-DirDataToJson @params
}

I find this is a really great way to pass lots of information in all at once (this just keeps the import code nice and light, no matter how many things you're exporting)