I am trying to pull a list of all Computer objects from our dozen or so AD domains in the Forest. it does that, but the problems is that the += is stripping out a number of properties. I tried using .add, but that results in errors when get-adcomputer only pulls up the name, and not IP or OS.
$domains = (Get-ADForest).domains
foreach ($domain in $domains) {
Get-ADComputer -Filter * -Properties ipv4Address, OperatingSystem -Server $Domain
$complist.add($computers)
echo $computers
}
$complist |Select-Object -Property Name, ipv4Address, OperatingSystem| Export-Csv -Path 'C:\Users\<redacted>\Documents\computerslist.csv' -NoTypeInformation
echo $complist
>Solution :
Its unclear what $compList is in your code but it is clear that is not needed to accomplish the task:
(Get-ADForest).Domains | ForEach-Object {
Get-ADComputer -Filter * -Properties ipv4Address, OperatingSystem -Server $_
} | Select-Object Name, ipv4Address, OperatingSystem |
Export-Csv -Path 'C:\Users\<redacted>\Documents\computerslist.csv' -NoTypeInformation
If you need output to the console for the queried computers you can add a call to Out-Host but this is only adding overhead to your code:
(Get-ADForest).Domains | ForEach-Object {
$computers = Get-ADComputer -Filter * -Properties ipv4Address, OperatingSystem -Server $_
$computers | Out-Host
$computers
} | Select-Object Name, ipv4Address, OperatingSystem |
Export-Csv -Path 'C:\Users\<redacted>\Documents\computerslist.csv' -NoTypeInformation