If you have a lot of computers to check then you'll want to introduce some way to do concurrent queries, especially if you have computers that may be offline or inaccessible.
Here's a version that used jobs to run up to 32 queries at the same time:
$cn = Get-ADComputer -Filter * | Select-Object -ExpandProperty Name
foreach ($name in $cn){
Start-Job -ScriptBlock {
$dd = Get-WmiObject -ClassName Win32_OperatingSystem -ComputerName $Using:name 2>&1 # redirect STDERR to STDOUT
# you should probably check the object type of $dd here to see if it's an ErrorRecord instead of
# blindly accepting it as the output that contains the data you expected!
# if it's an ErrorRecord you should build another PSCustomObject and place
# the result in either Version of Build
[PSCustomObject]@{
Name = $Using:name
Version = $dd.Version
Build = $dd.BuildNumber
}
} | Out-Null
}
Do{
Foreach ($j in (Get-Job -State Completed)) {
Receive-Job $j.Id |
Select-Object Name,Version,Build
Remove-Job $j.id | Out-Null
}
} While (Get-Job)