none
get-aduser -filter samaccountname returns distinguished name RRS feed

  • Question

  • When I run this from powershell command prompt:

    get-aduser -filter {samaccountname -eq 'testmvfm'}

    I get these results: 

    GivenName         : firstname
    Name              : testmvfm
    ObjectClass       : user
    SamAccountName    : testmvfm

    When I run this from a script I get the distinguished name.

    Snippet from script:

    foreach ($record in (import-csv ultiimport.csv)) {
    # $importuser = $record.LoginName

    $samfilter = $record.loginname
    $emailfilter = $record.emailaddress
    $user2 =get-aduser -filter {samaccountname -eq 'testmvfm'}
    write-host $user2

    Results:

    PS C:\SCRIPTS> .\ultiprotoad4.ps1
    CN=testmvfm,CN=Users,DC=fmhnt,DC=fmh,DC=org
    WARNING: user testmvfm not found
    CN=testmvfm,CN=Users,DC=fmhnt,DC=fmh,DC=org
    WARNING: user testrdp2 not found
    CN=testmvfm,CN=Users,DC=fmhnt,DC=fmh,DC=org
    WARNING: user testrdp2 not found
    PS C:\SCRIPTS>

    What the heck is going on???

    Friday, August 30, 2019 12:30 AM

Answers

  • If you turn a user object into a string it will return the DN. TO get any other property you must addd the attribute name.

    write-host $user2.SamAccountName

    This is basic PowerShell 101 and is learned when you take the tile to learn PowerShell.  It has nothing to do with AD, Excel or spreadsheets.  It is just how PowerShell works.


    \_(ツ)_/

    • Marked as answer by Kspencer66 Tuesday, September 24, 2019 7:26 PM
    Friday, August 30, 2019 3:28 AM

All replies

  • Just tried the script again with a different attribute...

    no matter what I use the results are the same..the distinguished name.

    Friday, August 30, 2019 12:48 AM
  • What exactly is your problem? If you want to have a specific output you use a Select-Object and specify what properties you want or you create a custom object.

    BTW: Please do not post colorized code .. use the code posting tool instead.

    And: if you use the sAMAccountName anyway you don't need to use the -Filter parameter for your Get-ADUser you can use the -Identity parameter.


    Live long and prosper!

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

    • Edited by BOfH-666 Friday, August 30, 2019 1:11 AM
    Friday, August 30, 2019 1:09 AM
  • I’m trying to match the samaccount name in my spreadsheet to samaccount in ad and set attributes if match. If you look at the results above for write host you will see it’s returning a dn instead of samaccountname so it’s not matching. Sorry don’t know what you mean colorized code.
    Friday, August 30, 2019 1:25 AM
  • If you want to see specific AD attributes you use Select-Object to specify what you want to see:
    Get-ADUser -Identity 'sAMAccountName' |
        Select-Object -Property GivenName,Name,ObjectClass,sAMAccountName


    Live long and prosper!

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

    Friday, August 30, 2019 1:32 AM
  • If you turn a user object into a string it will return the DN. TO get any other property you must addd the attribute name.

    write-host $user2.SamAccountName

    This is basic PowerShell 101 and is learned when you take the tile to learn PowerShell.  It has nothing to do with AD, Excel or spreadsheets.  It is just how PowerShell works.


    \_(ツ)_/

    • Marked as answer by Kspencer66 Tuesday, September 24, 2019 7:26 PM
    Friday, August 30, 2019 3:28 AM