Need to correct this AD script

Cristian Romance 56 Reputation points
2021-02-11T22:45:20.477+00:00

Hi,

I make this script. This script counts how many users have each OU into the two Principal OU's ( RDS Funcional and VDI Funcional). The script works fine but i need to remove the total count of this two principals OU's. In the Image wants to remove the line RDS Funcional,"723". There an other line with this VDI Funcional,"295"

I need to remove both lines

Also remove the double quotes

Exemple in the image This line is fine -> 4127 Transportes Corporativos,16

Thanks :)

Script:

 $log = @()  
          "OU=RDS Funcional,DC=esofitec,DC=loc","OU=VDI Funcional,DC=esofitec,DC=loc" | ForEach-Object {  
              Get-ADOrganizationalUnit -Filter * -SearchBase $_ | ForEach-Object {  
                  $log += "Processsing {0}" -f $_.distinguishedname     
                 [array] $users = Get-ADUser -Filter "Name -notlike 'test*' -and Name -notlike 'adm*'" -SearchBase $_.distinguishedname  
                  if ($users -ne $null) {  
                      $log += "{0} has {1}" -f $users.name, $users.count  
                      [PSCustomObject]@{  
                          OU    = $_.name  
                          Users = "{0}" -f $users.count  
                      }  
                   }  
              }  
          } | Export-Csv -Path c:\\ExportUsers.csv -NoTypeInformation -Delimiter ','  
          $log  

Example in an Image:

67154-imagen.png

67155-captura.png

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,527 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 46,796 Reputation points
    2021-02-12T03:14:49.75+00:00

    Does this fix your script?

    $log = @()
    "OU=RDS Funcional,DC=esofitec,DC=loc", "OU=VDI Funcional,DC=esofitec,DC=loc" | 
        ForEach-Object {
             $baseou = $_
            Get-ADOrganizationalUnit -Filter * -SearchBase $_ | 
                ForEach-Object {
                    if ($_.distinguishedname -ne $baseou){
                        $log += "Processsing {0}" -f $_.distinguishedname   
                        [array] $users = Get-ADUser -Filter "Name -notlike 'test*' -and Name -notlike 'adm*'" -SearchBase $_.distinguishedname -SearchScope OneLevel
                        if ($users -ne $null) {
                            $log += "{0} has {1}" -f $_.name, $users.count
                            [PSCustomObject]@{
                                OU    = $_.name
                                Users = $users.count
                            }
                        }
                    }
                }
        } | Export-Csv -Path c:\\ExportUsers.csv -NoTypeInformation -Delimiter ','
    $log
    

    EDIT: Move Export-CSV cmdlet.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Cristian Romance 56 Reputation points
    2021-02-12T07:15:06.07+00:00

    Hi RichMatheisen-8856 ,

    In your Fix, runs well but only counts the OU "VDI Funcional", the other OU "RDS Funcional" didn't appear.

    Can you fix both principal OU's?

    Thanks!

    Best.


  2. Cristian Romance 56 Reputation points
    2021-02-12T22:20:57.823+00:00

    Hi Rich!!

    Works fine :)

    Thank you very much :)

    Best!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.