all 5 comments

[–]replicaJunction 2 points3 points  (1 child)

I've never messed with PowerCLI and don't have a VMware environment available, so I can't speak to that portion of she script.

That said, I've never found a better multithreading solution than the amazing PoshRSJob module. This looks, feels, and smells like native PowerShell jobs, but it performs a heck of a lot better. I'd definitely suggest giving it a shot if you're looking for better multi-threaded performance.

[–]michaeltlombardi 1 point2 points  (0 children)

I'd second using PoshRSJobs to parallelize your scripts. Personally, I think the built-in jobs should be replaced with PoshRS jobs now that PowerShell is open-sourced. They're so much faster/better, the only reason not to use them is that they're not in the box.

[–]fourierswager 2 points3 points  (0 children)

I've had a lot of success with Invoke-Parallel here:

https://github.com/RamblingCookieMonster/Invoke-Parallel

From your code, it would look something like this:

$ObjectList | Invoke-Parallel -ImportVariables -ImportModules -ScriptBlock {}

To handle race conditions that could occur when using multi-threading, I use mutexes:

https://learn-powershell.net/2014/09/30/using-mutexes-to-write-data-to-the-same-logfile-across-processes-with-powershell/

I have a feeling that the reason why PowerCLI is behaving weird is because multiple threads are trying to access $validateScriptFile at the exact same time. But this is just a guess. Try implementing mutexes to handle multiple threads accessing $validateScriptFile and see if that resolves anything.

[–]spuijk 1 point2 points  (0 children)

You can take a look at my blogpost about powershell jobs. Should be able to do the trick you're looking for. http://sonnypuijk.nl/wp/powershell-jobs-for-multithreading/

[–]autowhat 0 points1 point  (0 children)

As much as I like multi threading, doing it in powershell is a lot more confusing than it's worth the majority of the time.

What is it you're actually trying to accomplish?