Trying to show AD computer objects per OU by name and OS

Hello /r/powershell I’m trying to edit the script below with the goal of getting a CSV that shows a list of computer objects per OU along with the name, count (in the case that there are duplicate for some reason), and OS of each computer object.

So something like Row 1: Computer Name | OU | OperatingSystem | Count

This may have to be done in two scripts, but I figure it wouldn’t hurt to ask if the logic I’m trying to follow makes sense. I also know that both OU and name can be derived from the DistinguishedName, I’m just unsure of how to properly format it so that it can separate into 2 things. I also believe my screwing up something somewhere.

Import-Module ActiveDirectory $OU = "OU=Desktops,OU=Office,OU=Departments,DC=my,DC=domain,DC=local" $ObjectOU = (Get-ADOrganizationalUnit -filter * -SearchBase $OU) % { $Count = (Get-ADComputer -SearchBase $OU -Filter {OperatingSystem} -Property * | ? {$_.Distinguishedname} | Measure).count If ($Count -ge 1){ New-Object PSobject -Property @{ OUDN = $_.DistinguishedName Object = $ObjectOU Count = $Count } } } | Select Object, OUDN,OperatingSystem, Count | Export-CSV \sharescriptsAD_Objects_Per_OU.csv 

submitted by /u/Hotfuzz750
[link] [comments]

Leave a Reply