First time poster and a PowerShell novice here. I was thinkering with a script yesterday and got it to work but feel like it can use some optimization. In particular, if I can do this with in a single Invoke-Command call. Any advise would be much appreciated. Here’s the relevant portion of the script:
Some recommendations: Read the Powershell Best Practice and Style Guide please. IMHO Code formatting is crucial for good scripts. Try to avoid too much white space.
You load a module you’re not using later, why? And actually you don’t need to load that in advance at all. Since Powershell verion 3 it will be autoloaded if needed.
You can combine 2 or more different queries in one Invoke-Command using PSCustomObject. You can access the single properties later like showed here above.
I tested with both Joel /u/ta11ow and kvprasoon's suggestions and in each case it worked great. The one thing that boggles me is that Joel’s format is tidier especially for objects with many properties but kvprasoon’s style makes the script a bit faster.
That’s 4 minutes and 33 seconds vs. 4 minutes and 23 seconds. Would that actually matter? I’d think that could be the measurement tolerance. Did you try it more than once?