I need to run the code below on about 200 client machines. The code works as I intended, but I’m wondering if there is a more efficient way to do this without using nested ForEach loops? Nesting loops just doesn’t seem efficient to me.
Any suggestions?
foreach ($Computer in $ComputerName){
############ Java Product Info
$JavaVer = Get-ChildItem "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" -Recurse |
ForEach-Object { Get-ItemProperty $_.pspath } | Where-Object DisplayName -Like "*Java 8*" | Select-Object DisplayVersion
}
In my use case scenario, is it safe to say the performance difference in all those options are probably negligible at best? I know I can use Measure-Command, but how accurate a measure of performance is that?
Something like:
$JavaVer = Invoke-Command $ListOfComputerNames {
Get-ChildItem "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" -Recurse |
ForEach-Object { Get-ItemProperty $_.pspath } | Where-Object DisplayName -Like "*Java 8*" | Select-Object DisplayVersion
}
Would also work just fine. Invoke-Command is going to be a lot faster than using a foreach loop with 200 computers. Invoke-Command takes an array of strings also so you can simply just give it every machine name at the same time. Give it a shot. Let us know how you go.