none
Import-csv to Foreach to get-aduser issues RRS feed

  • Question

  • Hello,

    I have the script below that is not giving me any output. I know it's an issue with me using $user for the filter in get-aduser, but I can't for the life of me figure out how to make it work. Any assistance would be greatly appreciated. The CSV file has no header, but is made up of email addresses in a single column.

    $users = import-csv C:\Filename.csv -Header user

    $results = ForEach ($user in $users)

    {

    get-aduser -filter {EmailAddress -eq "$user"} -properties CanonicalName,LastLogon | 
    select Name,@{N="Thing";E={$_.DistinguishedName.Split(',')[-4]}},@{N="Place";E={$_.DistinguishedName.Split(',')[-5]}},@{N="Last Logon";E `
    ={[DateTime]::FromFileTime($_.Lastlogon)}}

    }

    $results | Export-Csv C:\Filename.csv

    Thanks!

    Friday, June 9, 2017 8:38 PM

Answers

  • Start with

    help import-Csv -Full

    Read it carefully and look closely at the examples.

    A CSV file creates objects when imported. It is not a text list.


    \_(ツ)_/

    • Marked as answer by Spanderson Saturday, June 10, 2017 12:26 AM
    Saturday, June 10, 2017 12:18 AM

All replies

  • get-aduser -filter "EmailAddress -eq '$user'" -properties CanonicalName,LastLogon

    Quotes are required to expand variables.


    \_(ツ)_/

    Friday, June 9, 2017 9:25 PM
  • Hello,

    Thank you for the response. I just tried that, and I got a blank .csv file. Any other ideas?

    Thank you


    Friday, June 9, 2017 11:51 PM
  • If the email address is incorrect then it will be blank.

    If you don't have a CSV then your output will fail.  If you do have a CSV then you are loading it wrong.

    What is in your file?

    A CSV file is loaded like this:

    import-csv C:\Filename.csv


    \_(ツ)_/


    • Edited by jrv Saturday, June 10, 2017 12:01 AM
    Saturday, June 10, 2017 12:00 AM
  • I've got two email addresses which I know are correct. I'm loading the CSV in the first line with:

    $users = import-csv C:\Filename.csv -Header user

    The csv has no headers in it, it's just a column with two email addresses right now. I wanted to make sure it works before I start loading the .csv file with the list of email addresses I have.

    Thanks


    Saturday, June 10, 2017 12:06 AM
  • Start with

    help import-Csv -Full

    Read it carefully and look closely at the examples.

    A CSV file creates objects when imported. It is not a text list.


    \_(ツ)_/

    • Marked as answer by Spanderson Saturday, June 10, 2017 12:26 AM
    Saturday, June 10, 2017 12:18 AM
  • Thanks.

    I switched import-csv to get-content instead and it worked.

    Saturday, June 10, 2017 12:26 AM