none
Checking User in AD RRS feed

  • Question

  • Hi Scripting Guys,

    I am working on a script to automate a task, for that I created one function to check if a user exists in AD before executing the mail function, below is the code, but it seems that it is not working, could someone please help in this

    ===========

    Function User_Check ()

    {

    $List = @()

    $Users = Import-Csv -Path $FileName

    Foreach ($USer in $Users)

        {

    $User_Check = Get-ADUser -LDAPFilter "(sAMAccountName= $User.sAMAccountName)" -ErrorAction SilentlyContinue

    If ($User_Check -eq $Null) {$List += $User}

                    }

    if (($List).count -gt 1 )

    {Write-Host -ForegroundColor RED "Below Users already exist in AD $List"}

    else {Mail_Function}

    ======================   

    Monday, February 3, 2014 7:24 PM

Answers

  • Hi,

    Not working how?

    EDIT: Here's an example you can build from:

    Import-Csv .\userList.csv | ForEach {
    
        try {
    
            $userCheck = Get-ADUser $_.sAMAccountName -ErrorAction SilentlyContinue
    
        }
    
        catch {}
    
        If ($userCheck) { Write-Host "$($_.sAMAccountName) exists in AD." ; Remove-Variable userCheck }
    
        Else { Write-Host "$($_.sAMAccountName) does NOT exist in AD." }
    
    }


    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)


    • Edited by Mike Laughlin Monday, February 3, 2014 7:42 PM
    • Marked as answer by USNick Tuesday, February 4, 2014 11:32 AM
    Monday, February 3, 2014 7:28 PM
  • You guys always do this the hard way.

    Try this:

    Import-Csv .\userList.csv | 
         ForEach {
              $samname=$_.sAMAccountName
              if(Get-ADUser -Filter "SamAccountName='$samname'" ){
                   Write-Host "$samname exists in AD." -fore green
              }else{
                   Write-Host "$samname does NOT exist in AD."  -fore red
              }
         }
    No need to handle errors with a search.  It either finds the object or finds nothing.


    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, February 4, 2014 12:12 AM
    • Marked as answer by USNick Tuesday, February 4, 2014 10:24 AM
    Tuesday, February 4, 2014 12:10 AM

All replies

  • Hi,

    Not working how?

    EDIT: Here's an example you can build from:

    Import-Csv .\userList.csv | ForEach {
    
        try {
    
            $userCheck = Get-ADUser $_.sAMAccountName -ErrorAction SilentlyContinue
    
        }
    
        catch {}
    
        If ($userCheck) { Write-Host "$($_.sAMAccountName) exists in AD." ; Remove-Variable userCheck }
    
        Else { Write-Host "$($_.sAMAccountName) does NOT exist in AD." }
    
    }


    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)


    • Edited by Mike Laughlin Monday, February 3, 2014 7:42 PM
    • Marked as answer by USNick Tuesday, February 4, 2014 11:32 AM
    Monday, February 3, 2014 7:28 PM
  • You guys always do this the hard way.

    Try this:

    Import-Csv .\userList.csv | 
         ForEach {
              $samname=$_.sAMAccountName
              if(Get-ADUser -Filter "SamAccountName='$samname'" ){
                   Write-Host "$samname exists in AD." -fore green
              }else{
                   Write-Host "$samname does NOT exist in AD."  -fore red
              }
         }
    No need to handle errors with a search.  It either finds the object or finds nothing.


    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, February 4, 2014 12:12 AM
    • Marked as answer by USNick Tuesday, February 4, 2014 10:24 AM
    Tuesday, February 4, 2014 12:10 AM
  • You guys always do this the hard way.

    You say hard, I say fun. =]


    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)

    Tuesday, February 4, 2014 12:28 AM
  • Thanks to you both !!!

    I want to store all the existing users in a variable and want to show at one time before executing the mail function, rather than each time.

    Like :-

    If ($User_Check -eq $Null) {$List += $User}

            }

    if (($List).count -gt 1 )

    {Write-Host -ForegroundColor RED "Below Users already exist in AD $List"}

    else {Mail_Function}

    Any Comments?

    Tuesday, February 4, 2014 2:41 AM
  • Yes.  It won't work.

    Try doing what was suggested above.  it will work much better.

    If you don't understand the suggestions then start here: http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx


    ¯\_(ツ)_/¯

    Tuesday, February 4, 2014 5:27 AM
  • Thanks !!
    Tuesday, February 4, 2014 6:36 AM