locked
Check AD accounts from list of samaccountnames in csv RRS feed

  • Question

  • Hi ,

    I am trying to check if samaccountname exist  in AD,. my csv file has header samaccountname

    but it seems my variables are not working . any clues

    $data = Import-Csv -Path "C:\psdump\myusers.csv"

    foreach ($samaccountname in $data) {
       try {
          get-aduser -filter "samaccountname -eq $($_.samaccountname)" -erroraction stop
          write-host "User '$($_.samaccountname)' exists in AD."
          Out-file C:\psdump\exists.txt -append -inputobject "User '$($_.samaccountname)' exists in AD." `
        
          }
       catch {
          write-host "User '$($_.samaccountname)' DOES NOT exist in AD."
          Out-file -inputobject "User '$($_.samaccountname)' DOES NOT exist in AD." `
           C:\psdump\not-exists.txt -append
          }
       }


    • Edited by ezwizzy Friday, November 16, 2018 12:23 AM s
    Friday, November 16, 2018 12:19 AM

Answers

  • In the original script, if the CSV file has the header line sAMAccountName, and a value is jsmith, then $sAMAcountName will be @{sAMAccountName=jsmith}. The expression $($_.sAMAccountName) will be blank. But $sAMAccountName.sAMAccountName will be jsmith.

    Edit: My suggestion would be:

    $Data = Import-Csv -Path "C:\psdump\myusers.csv"
    ForEach ($Line in $Data)
    {
        $NTName = $Line.sAMAccountName
        $User = Get-ADUser -Identity $NTName
        If ($User)
        {
            Write-Host "User $NTName exists in AD."
            Out-file C:\psdump\exists.txt -append -inputobject "User $NTName exists in AD."
        }
        Else
        {
          Write-Host "User $NTName DOES NOT exist in AD."
          Out-file C:\psdump\not-exists.txt -append "User $NTName DOES NOT exist in AD."
        }
    }
    


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)


    • Edited by Richard MuellerMVP Friday, November 16, 2018 2:20 AM
    • Marked as answer by ezwizzy Friday, November 16, 2018 12:51 PM
    Friday, November 16, 2018 2:07 AM
  • In the original script, if the CSV file has the header line sAMAccountName, and a value is jsmith, then $sAMAcountName will be @{sAMAccountName=jsmith}. The expression $($_.sAMAccountName) will be blank. But $sAMAccountName.sAMAccountName will be jsmith.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Yes.  My error.  I copied some code and failed to update it.  I have fixed the original post.

    What Richard is saying is to change "$_" to "$SamAccountName" or whatever variable you used to enumerate the CSVwhich is why I prefer doing it this way:

    Import-Csv C:\psdump\myusers.csv |
        ForEach-Object{
            if(get-aduser -filter "samaccountname -eq '$($_.samaccountname)'"){
                $msgtxt =  "User $($_.samaccountname) exists in AD."
                write-host $msgtxt -Fore Green
                $msgtxt | Out-file C:\psdump\exists.txt -append
            }else{
                $msgtxt = "User $($_.samaccountname) DOES NOT exist in AD."
                write-host $msgtxt -fore Cyan
                $msgtxt | Out-file C:\psdump\not-exists.txt -append
            }
        }
    

    Understanding variables in all context is critical to using PowerShell.  My gross oversights certainly are not helpful.


    \_(ツ)_/


    • Edited by jrv Friday, November 16, 2018 2:13 AM
    • Marked as answer by ezwizzy Friday, November 16, 2018 12:51 PM
    Friday, November 16, 2018 2:12 AM

All replies

  • I suggest similar to:

    foreach ($Line in $data) {
       try {
          get-aduser -filter "samaccountname -eq $Line.samaccountname" -erroraction stop
    

    You need to refer to the field name in the header.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Friday, November 16, 2018 12:44 AM
  • Unfortunately you are both wrong.  This one is always a "gotcha" because of the filter's weirdness.

    Fist note that with a filter you will never get an exception.

    $data = Import-Csv C:\psdump\myusers.csv
    
    foreach ($samaccountname in $data) {
        if(get-aduser -filter "samaccountname -eq '$($samaccountname .samaccountname)'"){
            $msgtxt =  "User $($_.samaccountname) exists in AD."
            write-host $msgtxt -Fore Green
            $msgtxt | Out-file C:\psdump\exists.txt -append
        }else{
            $msgtxt = "User $($_.samaccountname) DOES NOT exist in AD."
            write-host $msgtxt -fore Cyan
            $msgtxt | Out-file C:\psdump\not-exists.txt -append
        }
    }
    Single quotes are required in the filter.


    \_(ツ)_/


    • Edited by jrv Friday, November 16, 2018 2:08 AM
    Friday, November 16, 2018 1:07 AM
  • still having problems... it seems the

     
    get-aduser : The search filter cannot be recognized
    At line:4 char:9
    +     if (get-aduser -filter "samaccountname -eq '$($_.samaccountname)' ...
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Get-ADUser], ADException
        + FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser

    Friday, November 16, 2018 1:43 AM
  • You obviously have a typo somewhere or there is no field named "SamAccountName" in your CSV.

    Post the first few lines of your CSV.

    Remember that a CSV has headers with column names and a very specific format.  It is not just a simple text file.  JUst adding a CSV extension to a file does not make it a CSV file.


    \_(ツ)_/


    • Edited by jrv Friday, November 16, 2018 1:47 AM
    Friday, November 16, 2018 1:46 AM
  • In the original script, if the CSV file has the header line sAMAccountName, and a value is jsmith, then $sAMAcountName will be @{sAMAccountName=jsmith}. The expression $($_.sAMAccountName) will be blank. But $sAMAccountName.sAMAccountName will be jsmith.

    Edit: My suggestion would be:

    $Data = Import-Csv -Path "C:\psdump\myusers.csv"
    ForEach ($Line in $Data)
    {
        $NTName = $Line.sAMAccountName
        $User = Get-ADUser -Identity $NTName
        If ($User)
        {
            Write-Host "User $NTName exists in AD."
            Out-file C:\psdump\exists.txt -append -inputobject "User $NTName exists in AD."
        }
        Else
        {
          Write-Host "User $NTName DOES NOT exist in AD."
          Out-file C:\psdump\not-exists.txt -append "User $NTName DOES NOT exist in AD."
        }
    }
    


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)


    • Edited by Richard MuellerMVP Friday, November 16, 2018 2:20 AM
    • Marked as answer by ezwizzy Friday, November 16, 2018 12:51 PM
    Friday, November 16, 2018 2:07 AM
  • In the original script, if the CSV file has the header line sAMAccountName, and a value is jsmith, then $sAMAcountName will be @{sAMAccountName=jsmith}. The expression $($_.sAMAccountName) will be blank. But $sAMAccountName.sAMAccountName will be jsmith.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Yes.  My error.  I copied some code and failed to update it.  I have fixed the original post.

    What Richard is saying is to change "$_" to "$SamAccountName" or whatever variable you used to enumerate the CSVwhich is why I prefer doing it this way:

    Import-Csv C:\psdump\myusers.csv |
        ForEach-Object{
            if(get-aduser -filter "samaccountname -eq '$($_.samaccountname)'"){
                $msgtxt =  "User $($_.samaccountname) exists in AD."
                write-host $msgtxt -Fore Green
                $msgtxt | Out-file C:\psdump\exists.txt -append
            }else{
                $msgtxt = "User $($_.samaccountname) DOES NOT exist in AD."
                write-host $msgtxt -fore Cyan
                $msgtxt | Out-file C:\psdump\not-exists.txt -append
            }
        }
    

    Understanding variables in all context is critical to using PowerShell.  My gross oversights certainly are not helpful.


    \_(ツ)_/


    • Edited by jrv Friday, November 16, 2018 2:13 AM
    • Marked as answer by ezwizzy Friday, November 16, 2018 12:51 PM
    Friday, November 16, 2018 2:12 AM
  • Awesome, you guys rock!

    Been working on this for 2 days :(

    If I may ask one more question. I am struggling to understand when to use double and single quotes any pointers would be appreciated

    Friday, November 16, 2018 12:51 PM
  • help about_Quoting_Rules

    single quotes are the primary quoting method in PowerShell even though non-trained people keep using double quotes. Double quoted strings are called expandable strings because the contents of the string are parsed by PowerShell and any key elements or expressions are expanded if possible.

    It is a good practice to avoid double quotes unless you really are needing the string to be altered (expanded) at runtime.


    \_(ツ)_/

    Friday, November 16, 2018 1:11 PM