Hi folks! I’m trying to master functions in powershell and I have a problem with my functions running queries “one-by-one”.
So I have this sample function which I want to collect data from several computers in Domain (see below).
I have noticed that this command
`'pc01', 'pc02', 'pc03' | get-diskspace `
queries pc01, gets result and only after that sends next query to pc02. As far as I know powershell is able to split this query into 3 and send it to all 3 computers concurrently.
So my questin is: what have I done wrong? Why it isn’t working as planned?
It won’t work concurrently. You need to specify some form of asynchrounous operation. For remote operations, i’d recommend instead performing the actions as jobs instead of using pipeline operations.
I’d typically do this within an Invoke-Command -Computername @(‘comp1’,‘comp2’) -Scriptblock {} -AsJob operation. At the end of your code perform a Get-Job | Wait-Job to pause until all remote jobs have completed. You can then use Receive-Job to gather the output.
In this particular case, it’s easier to just make one call to Get-WmiObject in your function’s End block, passing in all of the computer names. This is because Get-WmiObject already works concurrently when you give it multiple computer names, and you don’t have to do any of the work of managing that concurrency yourself.
I dont trust Invoke-Command because of two reasons:
First, it uses local instance of powershell and its version can vary anywhere between v2 and v5
Second, it makes impossible to use my favorite custom-build functions. And keeping track of cmdlets used in code may become a challenge
So, I guess I will have to dig into PSJobs. At least now I know that my goal cannot be achieved the way I have tried. Thanks again!