Find ADUsers with null value from import-csv (OUs)

This topic contains 16 replies, has 2 voices, and was last updated by Profile photo of Curtis Smith Curtis Smith 1 month ago.

Viewing 15 posts - 1 through 15 (of 17 total)
  • Author
    Posts
  • #50957
    Profile photo of Jeff Taylor
    Jeff Taylor
    Participant

    I have a list of OUs I have saved to a variable that I then want to search all users with a null valued attribute.

    I am not sure how to filter for the OU's already imported.

    Get-ADUser -SearchBase $allOUs -Filter * -Properties mailNickname | ? {$_.mailNickname -notlike "*"} | select samAccountName,DistinguishedName

    It has to do with my searchbase parameter I believe since I think PS is looking for an actual (single) OU, not a collection.

    How do I correct this please?

    #50959
    Profile photo of Curtis Smith
    Curtis Smith
    Participant
    $allOUs |
    ForEach-Object {
        Get-ADUser -SearchBase $_ -Filter * -Properties mailNickname |
        Where-Object {$_.mailNickname -notlike "*"} |
        select samAccountName,DistinguishedName
    }
    
    • This reply was modified 1 month, 1 week ago by Profile photo of Curtis Smith Curtis Smith.
    • This reply was modified 1 month, 1 week ago by Profile photo of Curtis Smith Curtis Smith.
    #50963
    Profile photo of Jeff Taylor
    Jeff Taylor
    Participant

    Hi Curtis, thanks

    I get this error for each row of my $allOUs variable:

    Get-ADUser : The supplied distinguishedName must belong to one of the following partition(s):'CN=Configuration,DC=co,DC=com , CN=Schema,CN=Configuration,DC=co,DC=com , DC=amer,DC=co,DC=com'.

    my $allOUs variable contains this format (no "")

    OU=Users1,OU=Vendors,DC=amer,DC=co,DC=com
    OU=Users2,OU=Vendors,DC=amer,DC=co,DC=com
    OU=Users3,OU=Vendors,DC=amer,DC=co,DC=com

    #51011
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    Very odd, something weird is being seen in the object. Whatever it is can be cropped of by adding .trim()

    $allOUs |
    ForEach-Object {
        Get-ADUser -SearchBase $_.trim() -Filter * -Properties mailNickname |
        Where-Object {$_.mailNickname -notlike "*"} |
        select samAccountName,DistinguishedName
    }
    
    #51018
    Profile photo of Jeff Taylor
    Jeff Taylor
    Participant

    hrm...again for each row in $allOUs:

    Method invocation failed because [System.Management.Automation.PSCustomObject] does not contain a method named 'Trim'.

    Are you sure it's not because my rows inside of $allOUs are not enclosed by double quotes?

    #51051
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    Certain, but I also don't know the data gets inside your variable at all. I assumed a standard list of OUs as an array of strings which is what you would get by using get-content to pull in data from a file, but you haven't really said how you are getting the data into $allOUs, and that could be where the problem is. (I know that's a run-on sentence, but I'm too tired to fix it right now)

    Here is the test I was working with

    $allOUs = @"
    CN=Users,DC=ca,DC=lab
    OU=CA,DC=ca,DC=lab
    CN=Users,DC=ca,DC=lab
    "@ -split "`r`n"
    
    $allOUs | 
    ForEach-Object {
        get-aduser -SearchBase $_.Trim() -Filter * | Select Name
    }
    

    Similarly, this works just fine without the trim

    $allOUs = get-content "C:\Users\Administrator\Desktop\OUs.txt"
    
    $allOUs | 
    ForEach-Object {
        get-aduser -SearchBase $_ -Filter * | Select Name
    }
    

    So the question really is, what is in your $allOUs variable because it's not just an array of strings.

    #51053
    Profile photo of Jeff Taylor
    Jeff Taylor
    Participant

    Ah i was using import-csv

    Let me try get-content tomorrow and advise back.

    Thanks

    #51058
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    ya, if you have a flat text file with just a list of OUs, and use Import-CSV, it's going to break your OU structure up based on the comma in the distinguished name into different columns, and by default used the first line as property names for the custom powershell object it creates, so instead of having a list like this in your $allOUs variable:

    $allOUs = get-content "C:\Users\Administrator\Desktop\OUs.txt"
    
    CN=Users,DC=ca,DC=lab
    OU=CA,DC=ca,DC=lab
    CN=Users,DC=ca,DC=lab
    

    Instead you end up with a collection of powershell custom object with the Distingushed name all broken up like this:

    $allOUs = import-csv "C:\Users\Administrator\Desktop\OUs.txt"
    
    CN=Users DC=ca DC=lab
    -------- ----- ------
    OU=CA    DC=ca DC=lab
    CN=Users DC=ca DC=lab
    

    The above OUs.txt file contains:

    CN=Users,DC=ca,DC=lab
    OU=CA,DC=ca,DC=lab
    CN=Users,DC=ca,DC=lab
    
    #51245
    Profile photo of Jeff Taylor
    Jeff Taylor
    Participant

    my OU csv has numerous rows of OUs throughout the domain.

    I ran:

    $allOUs=get-content -Path 'C:\Amer_OU.csv'

    I see the contents of the variable thus:

    "OU=OutsideVendors,OU=Vendors,DC=amer,DC=co,DC=com"
    "OU=Users,OU=Vendors,DC=amer,DC=co,DC=com"

    I run this:

    $AllOUs |
    ForEach-Object {
        Get-ADUser -SearchBase $_ -Filter * -Properties mailNickname |
        Where-Object {$_.mailNickname -notlike "*"} |
        select samAccountName,DistinguishedName
    }

    I get this error:

    Get-ADUser : The supplied distinguishedName must belong to one of the following partition(s): 'CN=Configuration,DC=co,DC=com , 
    CN=Schema,CN=Configuration,DC=co,DC=com , DC=amer,DC=co,DC=com'.

    I try with trim mthod and still get same above error.

    • This reply was modified 1 month, 1 week ago by Profile photo of Jeff Taylor Jeff Taylor.
    #51257
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    Ok, that means your file is in fact a single column CSV file. Does it have a header value?

    IE. Does the top of the file look something like this?

    "OU"
    "OU=OutsideVendors,OU=Vendors,DC=amer,DC=co,DC=com"
    "OU=Users,OU=Vendors,DC=amer,DC=co,DC=com"
    

    Or does it just start out as a list of OUs with no header value?

    "OU=OutsideVendors,OU=Vendors,DC=amer,DC=co,DC=com"
    "OU=Users,OU=Vendors,DC=amer,DC=co,DC=com"
    

    When you import-csv, the cmdlet returns a collection of PowerShell custom objects as previous discussed. The Column headers are the property names for each object, and each row of data is a different object.

    For Example, with the below CSV (OUs.csv):

    "OU"
    "CN=Users,DC=ca,DC=lab"
    "OU=CA,DC=ca,DC=lab"
    "CN=Users,DC=ca,DC=lab"
    

    Import-CSV would return 3 objects, each one with a Property called OU

    OU                   
    --                   
    CN=Users,DC=ca,DC=lab
    OU=CA,DC=ca,DC=lab   
    CN=Users,DC=ca,DC=lab
    

    When you do a ForEach-Object loop with that collection, $_ represents the current object, but if you want the OU value for that object, you have to specify that property like so. $_.OU

    $allOUs = import-csv "C:\Users\Administrator\Desktop\OUs.csv"
    
    $allOUs | ForEach-Object {
        $_.OU
    }
    

    So if you plug that back into your code, it would look something like this:

    $AllOUs |
    ForEach-Object {
        Get-ADUser -SearchBase $_.OU -Filter * -Properties mailNickname |
        Where-Object {$_.mailNickname -notlike "*"} |
        select samAccountName,DistinguishedName
    }
    

    If your CSV does not have column headers in the first line, then the first OU in the list will be used as the column header, unless you use the -header parameter when you use import-csv.

    For Example, with the below CSV (OUs.csv):

    "CN=Users,DC=ca,DC=lab"
    "OU=CA,DC=ca,DC=lab"
    "CN=Users,DC=ca,DC=lab"
    

    Import-CSV would return 3 objects, each one with a Property called OU since that is what you define with the -header parameter

    $allOUs = import-csv "C:\Users\Administrator\Desktop\OUs.csv" -Header "OU"
    
    $allOUs | ForEach-Object {
        $_.OU
    }
    
    • This reply was modified 1 month, 1 week ago by Profile photo of Curtis Smith Curtis Smith.
    #51809
    Profile photo of Jeff Taylor
    Jeff Taylor
    Participant

    Curtis,

    sorry for the late reply.

    I wasn't aware of the -header parmameter and true enough my csv did not contain it. Just row by row of OU values (and no quotes anywhere)

    So, I executed this for $allOUs:

    $allOUs = import-csv "C:\Users\Administrator\Desktop\OUs.csv" -Header "OU"

    and do see my output, row by row (again, no quotes anywhere) but when I plug this back into the original code you supplied:

    $AllOUs |
    ForEach-Object {
        Get-ADUser -SearchBase $_.OU -Filter * -Properties mailNickname |
        Where-Object {$_.mailNickname -notlike "*"} |
        select samAccountName,DistinguishedName
    }

    I get this new error:

    Get-ADUser : Cannot validate argument on parameter 'SearchBase'. The argument is null. Provide a valid value for 
    the argument, and then try running the command again.

    Here are the properties for $allOUs:

       TypeName: System.Management.Automation.PSCustomObject
    
    Name        MemberType   Definition                                                          
    ----        ----------   ----------                                                          
    Equals      Method       bool Equals(System.Object obj)                                      
    GetHashCode Method       int GetHashCode()                                                   
    GetType     Method       type GetType()                                                      
    ToString    Method       string ToString()                                                   
    OU          NoteProperty string OU=OU=DC,DC=amer,DC=co,DC=com
    • This reply was modified 1 month ago by Profile photo of Jeff Taylor Jeff Taylor. Reason: spelling
    • This reply was modified 1 month ago by Profile photo of Jeff Taylor Jeff Taylor. Reason: spelling
    #51845
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    Jeff,
    Please provide a sample of what is in your input file. The information seems to go back and forth as to whether or not there are double quotes in the input file. In the last update you said there are not, but in the previous update you showed where you did a get-content and the contents of the variable showed there were double quotes.

    Secondly, provide the exact code you are executing as one script. Are you executing the import-csv as one script and then running the foreach-object part as a different script? Are they the same script? Where are these pieces of code in relation to each other? Are you executing in a powershell prompt, or in a development environment like the ISE?

    Lastly, in the last update where you are showing the "properties for $allOUs", the Definition for OU shows a string that is not a Valid OU distinguished name. Providing a sample from the input file will help determine if the input data is incorrect or if something else is going on.

    #51981
    Profile photo of Jeff Taylor
    Jeff Taylor
    Participant

    Curtis, sorry for the confusion...

    There are "" when I open up the CSV in Notepad ++ but not when I open up in Excel nor when I call the variable. Header is now there per the earlier instruction.

    PS C:\> $allOUs
    
    OU                                                                                      
    --                                                                                      
    OU=Conference Rooms,OU=NKY,OU=DC,DC=amer,DC=co,DC=com                              
    OU=Users,OU=NKY,OU=DC,DC=amer,DC=co,DC=com                                         
    OU=ConferenceRooms,OU=ODC,OU=DC,DC=amer,DC=co,DC=com                               
    OU=Users,OU=ODC,OU=DC,DC=amer,DC=co,DC=com                                         
    OU=ConferenceRooms,OU=PDC,OU=DC,DC=amer,DC=co,DC=com                               
    OU=Users,OU=PDC,OU=DC,DC=amer,DC=co,DC=com

    I was doing this in the ISE but have since save the following code as a .ps1 and dot sourced it forma new PS(5) Admin console.

    $allOUs = Import-Csv -Path "C:\vNext deployment\mailNickname\Amer_OU.csv"
    
    ForEach-Object {
        Get-ADUser  -Searchbase $_.OU -Filter * -Properties mailNickname |
        Where-Object {$_.mailNickname -notlike "*"} |
        select samAccountName,DistinguishedName
    }

    I still get this error:

    PS C:\vNext deployment\mailNickname> .\emtpyNicknames.ps1
    Get-ADUser : Cannot validate argument on parameter 'SearchBase'. The argument is null. Provide a valid value for the argument, and
    then try running the command again.
    • This reply was modified 1 month ago by Profile photo of Jeff Taylor Jeff Taylor. Reason: spelling
    • This reply was modified 1 month ago by Profile photo of Jeff Taylor Jeff Taylor.
    #52006
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    Hi Jeff,

    You are not Piping your data into the ForEach-Object cmdlet. That is why $_.OU is null. There's no data.

    $allOUs = Import-Csv -Path "C:\vNext deployment\mailNickname\Amer_OU.csv"
    
    $allOUs |
    ForEach-Object {
        Get-ADUser  -Searchbase $_.OU -Filter * -Properties mailNickname |
        Where-Object {$_.mailNickname -notlike "*"} |
        select samAccountName,DistinguishedName
    }
    

    Or you can simplify it and not use a variable at all by piping the output of the Import-Csv cmdlet to the ForEach-Object cmdlet.

    Import-Csv -Path "C:\vNext deployment\mailNickname\Amer_OU.csv" |
    ForEach-Object {
        Get-ADUser  -Searchbase $_.OU -Filter * -Properties mailNickname |
        Where-Object {$_.mailNickname -notlike "*"} |
        select samAccountName,DistinguishedName
    }
    
    #52199
    Profile photo of Jeff Taylor
    Jeff Taylor
    Participant

    Yep I see that now, thanks!

    I tried the both options and piped into the foreach but get a different error. Get-ADUser is balking:

    Get-ADUser : Directory object not found
    At line:3 char:5
    +     Get-ADUser  -Searchbase $_.OU -Filter * -Properties mailNickname  ...
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (:) [Get-ADUser], ADIdentityNotFoundException

    Look at $allOUs | GM again:

       TypeName: System.Management.Automation.PSCustomObject
    
    Name        MemberType   Definition                                                          
    ----        ----------   ----------                                                          
    Equals      Method       bool Equals(System.Object obj)                                      
    GetHashCode Method       int GetHashCode()                                                   
    GetType     Method       type GetType()                                                      
    ToString    Method       string ToString()                                                   
    OU          NoteProperty string OU=OU=Conference Rooms,OU=NKY,OU=DC,DC=amer,DC=co,DC=com

    Look at that funky OU NoteProperty. Is that because of the Header in the csv I added?

    Here's the output of the $allOUs:

    
    OU                                                                                      
    --                                                                                      
    OU=Conference Rooms,OU=NKY,OU=DC,DC=amer,DC=co,DC=com                              
    OU=Users,OU=NKY,OU=DC,DC=amer,DC=co,DC=com  
Viewing 15 posts - 1 through 15 (of 17 total)

You must be logged in to reply to this topic.