header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Test existance of AD User account
Last Post 01 Nov 2011 12:41 AM by Jeremy Saunders. 15 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Not Resolved
GregUser is Offline
New Member
New Member
Posts:38
Avatar

--
01 Feb 2011 07:09 AM
    I am trying to figure out how to check if an AD user account exists. I am trying to use the New-ADUser cmdlet, but having a couple of issues. Do I use New-ADUser -filter or New-ADUser -LDAPFilter? Not sure what the real difference is. Also, how do I then test if the statement actually found an account or not?
    fr3ddUser is Offline
    New Member
    New Member
    Posts:34
    Avatar

    --
    01 Feb 2011 07:56 AM
    I would recommend that you look for an existing account first:

    $newAcctName = "somevalue" $exists = $null $exists = Get-ADUser -Identity $newAcctName if ($exists -ne $null) { # Create the new account }
    GregUser is Offline
    New Member
    New Member
    Posts:38
    Avatar

    --
    01 Feb 2011 08:44 AM
    Perfect! Thanks for the help.
    GregUser is Offline
    New Member
    New Member
    Posts:38
    Avatar

    --
    01 Feb 2011 01:07 PM
    One more uestion that I just noticed... When my script hits the "$exists = Get-ADUser -Identity $newAcctName" command, and the account doesn't exist, I receive a Powershell error message. Is there a way to surpress that error message?
    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    01 Feb 2011 11:00 PM
    Use the Filter parameter:

    if( -not (Get-ADUser -Filter {SamAccountName -eq 'somevalue'})) {'no such user,create the new account'}

    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    GregUser is Offline
    New Member
    New Member
    Posts:38
    Avatar

    --
    02 Feb 2011 12:18 PM
    Shay, I tried that and am receiving the following message:

    Get-ADUser : Property: 'Name' not found in object of type: 'System.Management.Automation.PSCustomObject'.

    This is the statement I am using:

    if ( -not (Get-ADUser -Filter {samAccountName -eq $_.Name}))

    I am importing a csv file so that si why the $_.Name.

    Also, what is the difference between Get-ADUser -Filter and Get-ADUser -LDAPFilter?
    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    02 Feb 2011 11:01 PM
    I think it's because $_.Name evaluates to null inside the filter scriptblock. Can you try this:

    ...
    $name = $_.Name
    if ( -not (Get-ADUser -Filter {samAccountName -eq $Name}))

    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    GregUser is Offline
    New Member
    New Member
    Posts:38
    Avatar

    --
    13 Feb 2011 12:31 PM
    Shay, I tried your code, but am receiving the following error message:

    Get-ADUser : Variable: 'Name' found in expression: $Name is not defined.
    At C:\Users\test\documents\powershell scripts\CreateAccounts.ps1:48 char:27
    + if (-not (Get-ADUser <<<< -Filter {samAccountName -eq $Name}))
    + CategoryInfo : InvalidArgument: (:) [Get-ADUser], ArgumentException
    + FullyQualifiedErrorId : Variable: 'Name' found in expression: $Name is not defined.,Microsoft.ActiveDirectory.Management.Commands.GetADUser
    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    13 Feb 2011 11:58 PM
    Sorry Greg, WRONG answer. Currently there's a bug with that syntax. The issue has been reported to the AD team.

    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    14 Feb 2011 12:13 AM
    Try with an LDAP filter:

    if ( -not (Get-ADUser -LDAPFilter "samaccountname=$($_.Name)"))
    {
    ...
    }



    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    GregUser is Offline
    New Member
    New Member
    Posts:38
    Avatar

    --
    15 Feb 2011 04:40 PM
    Shay, I am using your latest code and if the code resolves to true (the account doesn't exists), then I create it. So, I tested it trying to pass the account information for an account that does exist and it resolves to true and then errors out when I try to create an account that already exists. Looks like the same problem I have had. I cannot seem to test correctly for an AD account. Am I not understanding the code correctly? I assume the "-not" part of the statement means I am checking to see if the AD account doesn't exist in your statement above. Correct?
    fr3ddUser is Offline
    New Member
    New Member
    Posts:34
    Avatar

    --
    15 Feb 2011 06:31 PM
    The syntax for an LDAP filter is not quite correct, try this:
    $account = "fr3dd" Import-Module ActiveDirectory if ( -not (Get-ADUser -LDAPFilter "(sAMAccountName=$account)")) { Write-Host "Create the account!" } else { Write-Host "Already there" }
    LDAP filters need to have the parentheses to function properly. Also, I always make sure that the attribute names are in the appropriate case.

    HTH - fr3dd
    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    15 Feb 2011 10:56 PM
    Yeap, I've missed the parenthesis, thanks fr3dd.

    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    cameronoveUser is Offline
    Basic Member
    Basic Member
    Posts:352
    Avatar

    --
    16 Feb 2011 01:00 PM
    The quest tools don't seem to have this problem.

    This works:
    if(Get-QADUser $someuser){'Found User'}else{'Didn't find user'}

    I tried with a real account and a fake account. It worked as written.
    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    16 Feb 2011 10:45 PM
    Yet another advantage of the QAD snap-in over the AD module :)

    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    Jeremy SaundersUser is Offline
    New Member
    New Member
    Posts:1
    Avatar

    --
    01 Nov 2011 12:41 AM
    Just wanted to update this thread and mention that the same bug with Get-ADUser using the -Filter parameter also exists with Get-ADGroup. Use -LDAPFilter as suggested.

    Cheers,
    Jeremy.
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer