locked
Powershell script displays Disabled and enabled Users - Receiving error that user does not exist RRS feed

  • Question

  • Hello,

    I currently have a PS script that shows Disabled and Enabled users within Active Directory. Everything works fine; except for the fact if the user does not exist, it will display the following error within the console:

    Get-ADUser : Cannot find an object with identity: 'test1'

    I am green with PS and I need help adding an else if statement. If the user is not found, I want it to show "User not found". My results are exported to a csv file.  

    Here is my code:

    $userlist = Import-Csv "C:\Scripts\AD User Status\users1.csv"
    foreach($user in $userlist){
        Get-ADUser -Server "My Domain" -Identity $user.username -Properties * | Select DisplayName, SAMAccountName, Enabled |
        Export-Csv "C:\Scripts\AD User Status\UserStatus.csv" -NoTypeInformation -Append
        }

    I appreciate your help!

    :)


    • Edited by BryGuy77 Friday, August 24, 2018 12:04 PM
    Friday, August 24, 2018 12:03 PM

Answers

  • Thanks for all your help!

    I was able to get the code working the way I wanted it.

    Here is the code:

    $UserCSV   = 'C:\Scripts\AD User Status\users1.csv'
    $OutputCSV = 'C:\Scripts\AD User Status\UserStatus.csv'
    $checkpath = test-path -path "C:\Scripts\AD User Status\UserStatus.csv"
    
    If ($checkpath -eq "True" ) 
    {
    Remove-Item -path "C:\Scripts\AD User Status\UserStatus.csv"
    }
    sleep -Seconds 3
    $Users = Import-Csv -Path $UserCSV |
        Select-Object -ExpandProperty SamAccountName
    
    $Results =foreach( $User in $Users ){
        $Account = Get-ADUser -Server "My Domain" -Filter { SamAccountName -eq $User } -Property displayname, SamAccountName, Enabled
    
        if( $Account ){
            $Account | Select-Object -Property displayname, SamAccountName, Enabled
            }
        else{
            [pscustomobject]@{
                SamAccountName = $User
                Enabled        = 'Account Not Found'
                displayname = "User not found"
                }
            }
        }
    
    $Results | Export-Csv -Path $OutputCSV -NoTypeInformation

    THANKS AGAIN!

    • Marked as answer by BryGuy77 Friday, August 24, 2018 4:37 PM
    Friday, August 24, 2018 4:37 PM

All replies

  • Use a try/catch block for Get-ADUser with -ErrorAction Stop. Catch the error and modify it to your liking:

    $userlist = Import-Csv "C:\Scripts\AD User Status\users1.csv"
    foreach ($user in $userlist) {
        try {
            Get-ADUser -Server "My Domain" -Identity $user.username -Properties * -ErrorAction Stop | 
                Select DisplayName, SAMAccountName, Enabled |
                Export-Csv "C:\Scripts\AD User Status\UserStatus.csv" -NoTypeInformation -Append
        }
        catch {
            if ($_.CategoryInfo.Category -eq 'ObjectNotFound') {
                Write-Output "User $($user.username) not found" |
                    Out-File "C:\Scripts\AD User Status\UserStatus.csv" -Append
            }
            else {
                Write-Error "An unexpected error occured: $($_.Exception.Message)" -ErrorAction Continue
            }
        }
    }


    • Edited by John Seerden Friday, August 24, 2018 1:28 PM Fix single quotes do double quotes
    Friday, August 24, 2018 12:17 PM
  • Hi,

    you can use Try/Catch, as John said.

    You also can test if the user exists before export the output to csv. For example

    $user = Get-Aduser -Server "My Domain" -Identity $user.username -Properties * | Select DisplayName, SAMAccountName, Enabled
    if($user -eq $null){
      write-output "does not exist"
    }
    else{
      $user | Export-Csv "C:\Scripts\AD User Status\UserStatus.csv" -NoTypeInformation -Append
    }


    Friday, August 24, 2018 12:35 PM
  • Neither of these answer will work with AD CmdLet when specifying an identity.   You cannot trap on this exception.  You must use a filter to test for a users.

    Import-Csv 'C:\Scripts\AD User Status\users1.csv' |
        ForEach-Object{
            if($u = Get-ADUser-Identity -Filter "SamAccountName -eq '$($_.username)'" -Properties DisplayName){
                $u    
            }else{
                Write-Host 'User $($_.username) not found' 
            }
        } |
        Select DisplayName, SAMAccountName, Enabled |
        Export-Csv 'C:\Scripts\AD User Status\UserStatus.csv' -NoTypeInformation -Append
    


    \_(ツ)_/

    • Proposed as answer by clayman2 Friday, August 24, 2018 2:10 PM
    Friday, August 24, 2018 12:58 PM
  • Neither of these answer will work with AD CmdLet when specifying an identity.   You cannot trap on this exception.  You must use a filter to test for a users.

    Import-Csv 'C:\Scripts\AD User Status\users1.csv' |
        ForEach-Object{
            if($u = Get-ADUser-Identity -Filter "SamAccountName -eq '$($_.username)'" -Properties DisplayName){
                $u    
            }else{
                Write-Host 'User $($_.username) not found' 
            }
        } |
        Select DisplayName, SAMAccountName, Enabled |
        Export-Csv 'C:\Scripts\AD User Status\UserStatus.csv' -NoTypeInformation -Append


    \_(ツ)_/

    What makes you think you cannot trap these exceptions? Did you try it?

    • Marked as answer by BryGuy77 Friday, August 24, 2018 4:35 PM
    • Unmarked as answer by BryGuy77 Friday, August 24, 2018 4:37 PM
    Friday, August 24, 2018 1:33 PM
  • Thanks for all your help!

    I was able to get the code working the way I wanted it.

    Here is the code:

    $UserCSV   = 'C:\Scripts\AD User Status\users1.csv'
    $OutputCSV = 'C:\Scripts\AD User Status\UserStatus.csv'
    $checkpath = test-path -path "C:\Scripts\AD User Status\UserStatus.csv"
    
    If ($checkpath -eq "True" ) 
    {
    Remove-Item -path "C:\Scripts\AD User Status\UserStatus.csv"
    }
    sleep -Seconds 3
    $Users = Import-Csv -Path $UserCSV |
        Select-Object -ExpandProperty SamAccountName
    
    $Results =foreach( $User in $Users ){
        $Account = Get-ADUser -Server "My Domain" -Filter { SamAccountName -eq $User } -Property displayname, SamAccountName, Enabled
    
        if( $Account ){
            $Account | Select-Object -Property displayname, SamAccountName, Enabled
            }
        else{
            [pscustomobject]@{
                SamAccountName = $User
                Enabled        = 'Account Not Found'
                displayname = "User not found"
                }
            }
        }
    
    $Results | Export-Csv -Path $OutputCSV -NoTypeInformation

    THANKS AGAIN!

    • Marked as answer by BryGuy77 Friday, August 24, 2018 4:37 PM
    Friday, August 24, 2018 4:37 PM
  • Too much unnecessary code for this:

    $UserCSV   = 'C:\Scripts\AD User Status\users1.csv'
    $OutputCSV = 'C:\Scripts\AD User Status\UserStatus.csv'
    
    Import-Csv -Path $UserCSV |
        ForEach-Object{
            if($account = Get-ADUser -Server "My Domain" -Filter "SamAccountName -eq '$($_.Name)'" -Property displayname){
                $account
            }else{
                [pscustomobject]@{
                    SamAccountName = $_.Name
                    Enabled        = 'Account Not Found'
                    DisplayName = "User not found"
                }
            }
        } |
        Select-Object displayname, SamAccountName, Enabled |
        Export-Csv -Path $OutputCSV -NoTypeInformation
    Good design eliminate a lot of code that can make debugging and upgrading much harder.


    \_(ツ)_/

    Friday, August 24, 2018 4:47 PM
  • Thanks. I will try to keep that in mind for future scripts.  But for now it works perfectly.
    Friday, August 24, 2018 5:00 PM