sheldonhull / sheldonhull.hugo

hugo repo full of magical blog goodness with an interspersing of 🌮🌮🌮
https://www.sheldonhull.com
Other
6 stars 2 forks source link

Parallel Powershell for Running SQL - sheldonhull.com #88

Closed utterances-bot closed 2 years ago

utterances-bot commented 3 years ago

Parallel Powershell for Running SQL

https://www.sheldonhull.com/blog/parallel-powershell-for-running-sql/

taksis commented 3 years ago

Well. It seems that Invoke-Parallel is not supported anymore.

Invoke-Parallel : At line:10 char:5
+     foreach -parallel ($s in $ServerInstance)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The '-parallel' parameter can be used only within a workflow.
At line:1 char:34
+ ... 68.0.$_"} | Invoke-Parallel { [PSCustomObject] @{IP=$_;Result=echo $_ ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ParserError: (:) [Invoke-Parallel], ParseException
    + FullyQualifiedErrorId : ParallelNotSupported,PSParallel.InvokeParallelCommand

The same error using https://github.com/powercode/PSParallel example:

# ping all machines in a subnet
(1..255).Foreach{"192.168.0.$_"} | Invoke-Parallel { [PSCustomObject] @{IP=$_;Result=ping.exe -4 -a -w 20 $_}}
sheldonhull commented 3 years ago

@taksis I'm really sorry I missed this comment. So my first tip would be don't use Invoke-Parallel anymore. Try looking at this microblog post and see that example.

I really don't think using Invoke-Parallel or Start-RSJob is needed anymore with PowerShell 7 and the native support for parallel operations. It's much easier now. The more detailed write-up btw of the PSParallel style stuff was in: Quick Way to Run PowerShell in Parallel. You can see in there I ran into a lot of issues.

Last tip. Similar to Python, I feel that multithreading is not a first class citizen for PowerShell. It works well in the new 7+ version, but overall consider it much more complicated to debug and work with. For file transfers and things like that, perfect use case. For me I avoid it whenever possible and use it as a last resort to avoid extra complexity. Hope that helps!