PowerShell script dont works fine. Can you help me? :)

Cristian Romance 56 Reputation points
2021-01-11T10:36:58.77+00:00

Hi guys,
I have a script that get a list of each OU and his users number without "test and amd". The scripts works fine but in the case that an OU have only one user the script prints this text -> Microsoft.ActiveDirectory.Management.ADPropertyValueCollection users.What is the reason? How can correct this error?

    "OU=RDS Funcional,DC=esofitec,DC=loc","OU=VDI Funcional,DC=esofitec,DC=loc" | ForEach-Object {
        Get-ADOrganizationalUnit -Filter * -SearchBase $_ | ForEach-Object {
            $users = Get-ADUser -Filter "Name -notlike 'test*' -and Name -notlike 'adm*'" -SearchBase $_.distinguishedname

            [PSCustomObject]@{
                OU    = $_.name
                Users = "{0} users" -f $users.count
            }
        }
    } | Export-Csv -Path c:\test.csv -NoTypeInformation -Delimiter ','

Thanks and Best!

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,537 questions
0 comments No comments
{count} vote

Accepted answer
  1. Ian Xue 37,276 Reputation points Microsoft Vendor
    2021-01-11T14:14:00.807+00:00

    Hi,

    When there's only one user in the OU, $users in line 3 is not an array and doesn't have a property of count. You can specify it as an array

    [array]$users = Get-ADUser -Filter "Name -notlike 'test*' -and Name -notlike 'adm*'" -SearchBase $_.distinguishedname  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. MotoX80 33,996 Reputation points
    2021-01-11T13:42:43.103+00:00

    but in the case that an OU have only one user

    What statement is failing? I would think that the problem would be where there are no users in the OU. Try testing the $users variable for null. I also like to add logging to any code that I write. Due to the way that you are outputting the custom object, I think that you will need to append to a variable for logging.

    $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     
             $users = Get-ADUser -Filter "Name -notlike 'test*' -and Name -notlike 'adm*'" -SearchBase $_.distinguishedname  
             if ($users -ne $null) {  
                 $log += "{0} has {1} users." -f $users.name, $users.count  
                 [PSCustomObject]@{  
                     OU    = $_.name  
                     Users = "{0} users" -f $users.count  
                 }  
              }  
         }  
     } | Export-Csv -Path c:\test.csv -NoTypeInformation -Delimiter ','  
     $log  
    
    
    
    
    
    
    
     
    
    1 person found this answer helpful.
    0 comments No comments

  2. Cristian Romance 56 Reputation points
    2021-01-11T14:14:53.9+00:00

    Hi,

    I print the export file, the ou's with only one user print a text. And the other ou's works fine:

    Wxample:

    3163-03 Jose Maria Garcia Frau 14 users
    4529 Bombeos Palamos 2 users
    3163-04 Administraciones Calvia 9 users
    3163-05 Alejandra Iser Toral Microsoft.ActiveDirectory.Management.ADPropertyValueCollection users
    4539 Casals Fusteria Microsoft.ActiveDirectory.Management.ADPropertyValueCollection users

    Best!

    1 person found this answer helpful.
    0 comments No comments

  3. Cristian Romance 56 Reputation points
    2021-01-14T12:50:33.897+00:00

    Hi,

    In the same Script how can skip the word "users". I need to print only the OU and the number of users in that OU without the word "users"

    Example:

    Wrong:

    3163-03 Jose Maria Garcia Frau 14 users
    4529 Bombeos Palamos 2 users
    3163-04 Administraciones Calvia 9 users

    Want to:

    3163-03 Jose Maria Garcia Frau 14
    4529 Bombeos Palamos 2
    3163-04 Administraciones Calvia 9

    Thanks :)

    Best.

    1 person found this answer helpful.
    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.