locked
Trying to validate Active Directory users from a CSV File RRS feed

  • Question

  • We received a CSV file from SAP (over 300 users in file) to determine if they have valid AD Accounts.I did a sample query with 5 users

    Two Questions

    When i run script below i have 5 users (that is whawt we want) however there is a dotted line between Adam.Guira and David Cook 
    Question One => Why is that ?
    PS C:\Windows\system32> import-csv -path c:\Temp\sapusers2.csv

    Adam.Guira    
    ----------    
    David.Cook    
    Karen.Lowes   
    Marla.Kelly   
    Melissa.Rotella

    import-csv -path c:\Temp\sapusers2.csv | foreach-object { get-aduser -filter { name -like "*"}}

    the above command reports back on all users in Active directory - not just the five users in the sapusers2.csv 

    Question two = > What is wrong with the script?

    Thursday, August 17, 2017 3:30 PM

Answers

  • Hi Christopher Ellis 

    The top row of the CSV is taken as the header when you use import-CSV.  You can use a header like SamAccountName or Name. It should look like the below. 

    SamAccountName
    Adam.Guira
    David.Cook
    Karen.Lowes
    Marla.Kelly
    Melissa.Rotella

    you can use get-aduser and the $_. the headding name to search for the users.  Instead of using -filter.

    below is if you use samaccountname as the header 

    import-csv -path c:\Temp\sapusers2.csv | foreach-object { get-aduser $_.SamAccountName | select Name}
    

    or if you use name as the header it would need to be 

    import-csv -path c:\Temp\sapusers2.csv | foreach-object { get-aduser $_.Name | select Name}


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Thursday, August 17, 2017 6:02 PM

All replies

  • Probably you miss the headers in your csv. You will have to add them while importing it. See the help for guidance how to do it. Get-Help Import-CSV

    For your second question: You have to reference the pipeline element in your foreach-object loop ... like this:

     foreach-object { get-aduser -filter " name -like '$_.Name*'"}
    For this you need a column with a name in your csv. I would recommend to use the samaccountname instead if availabe.

    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''



    • Edited by BOfH-666 Thursday, August 17, 2017 3:39 PM
    Thursday, August 17, 2017 3:35 PM
  • Hi Christopher Ellis 

    The top row of the CSV is taken as the header when you use import-CSV.  You can use a header like SamAccountName or Name. It should look like the below. 

    SamAccountName
    Adam.Guira
    David.Cook
    Karen.Lowes
    Marla.Kelly
    Melissa.Rotella

    you can use get-aduser and the $_. the headding name to search for the users.  Instead of using -filter.

    below is if you use samaccountname as the header 

    import-csv -path c:\Temp\sapusers2.csv | foreach-object { get-aduser $_.SamAccountName | select Name}
    

    or if you use name as the header it would need to be 

    import-csv -path c:\Temp\sapusers2.csv | foreach-object { get-aduser $_.Name | select Name}


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Thursday, August 17, 2017 6:02 PM
  • Yes, the issue it's the header of your CSV file, you need to add a property for example "SamAccountName" as geoff.phillis suggest
    Friday, August 18, 2017 2:13 AM
  • Yes, the issue it's the header of your CSV file, you need to add a property for example "SamAccountName" as geoff.phillis suggest

    I recommend reading the whole thread before adding an answer.  You might find some surprises.


    \_(ツ)_/

    Friday, August 18, 2017 2:18 AM