Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Powershell += is stripping out data, and .add results in many errors

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading