This isn't the most efficient way to do this:
Get-ADComputer -SearchBase "OU=Computers,DC=AAA,DC=local" -Filter -Properties |
Where-Object { $_.operatingsystem -like "*7*" } |
Select-Object -Expand name | # Collect all computers from OU with WIN7
ForEach-Object {
$r = [PSCustomObject]@{
Name = $_
CPU = ""
RAM = ""
HDD = ""
OnLine = $false
}
if ( (Test-Connection $_ -Count 1 -Quiet -WarningAction SilentlyContinue)) { # Check availability
$r.Online = $true
$r.HDD = (Get-WmiObject -Computer $_ -Class Win32_DiskDrive | Select-Object Expand model)
$r.CPU = (Get-WmiObject -Computer $_ -Class win32_processor | Select-Object Expand name)
$r.RAM = (Get-WmiObject -Computer $_ -Class Win32_PhysicalMemory | Select-Object Expand capacity)
$r
}
Else {
$r
}
} | Export-CSV x:dir\name.csv -NoTypeInformation
A better way to do this would be to use Invoke-Command and provide an array of machines as the argument to the -Computer parameter. The script block would (as in the code above) would return all the information. You would have to redirect then ERROR stream on the Invoke-Command (e.g., 2>&1) and check each of the returned array elements to see if it was an [ErrorRecord] object to identify a machine that cannot be contacted. All the other items in the array would have the information you seek.