none
Cannot get AD User Account using Get-ADUser from user list or array. RRS feed

  • Question

  • Hi,

    I'm trying to catch Get-ADUser error from users list or array to identify if a user exist and if it doesn't exist, migrate that with ADMT.

    I created a GUI with a TextBox in powershell where I put the SamAccountName for users to search, then I created and array with the usernames with the following code:

    function Verify-User()
    {
        #Call GUI functions to input the usernames
        $users = Read-MultiLineInputBoxDialog -Message "Input users to verify:" -WindowTitle "User Verifier" -DefaultText $null
        $usersfmt = $users.Split("`n")    
        foreach($user in $usersfmt)
        {
            try
            {
                Write-Warning "Verifyng user $user"
                Get-ADUser $user
            }
            catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]
            {
                Write-Host -ForegroundColor Green "$user can be migrated because it doesnt exist in target domain."
            }
        }            
    }
    
    Verify-User

    When I run the code the script ever catch an error even if the user exist.

    Could you help me please why it happens?

    Tuesday, June 28, 2016 9:48 PM

Answers

  • Works fine for me.

    You didn't use my version or you would have had an error. 

    Run the following at a command prompt to verify that it works:

    function Verify-User() {
    	Param([string[]]$users)
    	
    	foreach ($user in $users) {
    		try {
    			Write-Warning "Verifyng user $user"
    			Get-ADUser $user -ErrorAction Stop
    		} catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]
    		{
    			Write-Host -ForegroundColor Green "$user can be migrated because it doesnt exist in target domain."
    		}
    	}
    }
    $users='usersA,UserB,UserC'
    $usersfmt = $users.Split(',')
    Verify-User $userfmt
    Copy and paste is at a CLI prompt.  Only change the users names.


    \_(ツ)_/

    • Marked as answer by JRLOPS Wednesday, June 29, 2016 5:02 PM
    Wednesday, June 29, 2016 4:56 PM
  • Here is how to get an array from a GUI:

    # run this and type in some lines then use forms X to close
    Add-Type -AssemblyName System.Windows.Forms
    $form1=New-Object System.Windows.Forms.Form
    $form1.StartPosition='CenterScreen'
    
    $textbox1=New-Object System.Windows.Forms.TextBox
    $form1.Controls.Add($textbox1)
    $textbox1.Multiline=$true
    $textbox1.Dock='Fill'
    
    $form1.ShowDialog()
    $textbox1.Lines
    


    \_(ツ)_/

    • Marked as answer by JRLOPS Wednesday, June 29, 2016 7:53 PM
    Wednesday, June 29, 2016 5:17 PM
  • Change:

      return $form.Tag

    to:

      return $textbox.Lines


    \_(ツ)_/

    • Marked as answer by JRLOPS Wednesday, June 29, 2016 8:39 PM
    Wednesday, June 29, 2016 8:13 PM

All replies

  • You can't trap errors on Get-AdUser.

    Use

    if(Get-Aduser -Filter "SamAccountName -eq '$user'"){
        'user found'
    }else{
        'user not found'
    }


    \_(ツ)_/

    Tuesday, June 28, 2016 10:00 PM
  • Thanks jrv,

    But if I put the username manual and not into variable it works and the the try catch stament works fine.

    In what cases did you said that Get-ADUser cmdlet doesn't work?

    Wednesday, June 29, 2016 4:16 PM
  • Thanks jrv,

    But if I put the username manual and not into variable it works and the the try catch stament works fine.

    In what cases did you said that Get-ADUser cmdlet doesn't work?

    So it does in WMF 5.  I wonder when they fixed that.

    So what is the issue?


    \_(ツ)_/


    • Edited by jrv Wednesday, June 29, 2016 4:24 PM
    Wednesday, June 29, 2016 4:19 PM
  • So it does in WMF 5.  I wonder when they fixed that.

    Interesting. This seems to work again in WMF 4 too.

    PS C:\Scripts\PowerShell Scripts\Misc Testing\6-29-2016> try {
        Get-ADUser doesnotexist
    } catch {
        Write-Host 'caught'
    }
    caught

    No idea when that was fixed.


    Wednesday, June 29, 2016 4:27 PM
  • I am guessing the fix was to the Net Framework which gets delivered with WU.


    \_(ツ)_/

    Wednesday, June 29, 2016 4:31 PM
  • Try it this way as this will eliminate many corner case issues:

    function Verify-User() {
    	Param([string[]]$users)
    	
    	foreach ($user in $users) {
    		try {
    			Write-Warning "Verifyng user $user"
    			Get-ADUser $user -ErrorAction Stop
    		} catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]
    		{
    			Write-Host -ForegroundColor Green "$user can be migrated because it doesnt exist in target domain."
    		}
    	}
    }
    #Call GUI functions to input the usernames
    #$users = Read-MultiLineInputBoxDialog -Message "Input users to verify:" -WindowTitle "User Verifier" -DefaultText $null
    $users='usersA,UserB,UserC'
    $usersfmt = $users.Split("`n")
    Verify-User $userfmt
    The test line will eliminate issues from the code we cannot see.


    \_(ツ)_/

    Wednesday, June 29, 2016 4:32 PM
  • Thank jrv,

    I'm trying with a list of users like that:

    usernotexists1

    usernotexists2

    userexists1

    userexist2

    when the script ends, all the users appear to be not existent. and finally I can see that only the last username can be evaluate fine.


    Additionally with the if else stament the evaluation cannot work.
    • Edited by JRLOPS Wednesday, June 29, 2016 4:53 PM
    Wednesday, June 29, 2016 4:52 PM
  • Works fine for me.

    You didn't use my version or you would have had an error. 

    Run the following at a command prompt to verify that it works:

    function Verify-User() {
    	Param([string[]]$users)
    	
    	foreach ($user in $users) {
    		try {
    			Write-Warning "Verifyng user $user"
    			Get-ADUser $user -ErrorAction Stop
    		} catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]
    		{
    			Write-Host -ForegroundColor Green "$user can be migrated because it doesnt exist in target domain."
    		}
    	}
    }
    $users='usersA,UserB,UserC'
    $usersfmt = $users.Split(',')
    Verify-User $userfmt
    Copy and paste is at a CLI prompt.  Only change the users names.


    \_(ツ)_/

    • Marked as answer by JRLOPS Wednesday, June 29, 2016 5:02 PM
    Wednesday, June 29, 2016 4:56 PM
  • Yes, thank so much jrv it works.

    Finally there'r something strange with the $users.split("`n"), because I verified that not special chars exist at the beginning or end of the username string, because I tried with string trim too, but that way don't work.

    So know that you said. I going to format the string array and then split by ",".

    Thanks so much again.

    Wednesday, June 29, 2016 5:02 PM
  • Why would a GUI string have new line characters in it?  Textboxes do not insert newline characters.

    How are you getting this string?


    \_(ツ)_/

    Wednesday, June 29, 2016 5:04 PM
  • A multiline textbox has lines:

    $lines=$textbox.Lines

    "$lines" will be an array.

    $textbox.Text has a string that has a single feed character between each line.


    \_(ツ)_/

    Wednesday, June 29, 2016 5:06 PM
  • Here is how to get an array from a GUI:

    # run this and type in some lines then use forms X to close
    Add-Type -AssemblyName System.Windows.Forms
    $form1=New-Object System.Windows.Forms.Form
    $form1.StartPosition='CenterScreen'
    
    $textbox1=New-Object System.Windows.Forms.TextBox
    $form1.Controls.Add($textbox1)
    $textbox1.Multiline=$true
    $textbox1.Dock='Fill'
    
    $form1.ShowDialog()
    $textbox1.Lines
    


    \_(ツ)_/

    • Marked as answer by JRLOPS Wednesday, June 29, 2016 7:53 PM
    Wednesday, June 29, 2016 5:17 PM
  • Hi jrv,

    I'm getting the string with the function described at http://blog.danskingdom.com/powershell-multi-line-input-box-dialog-open-file-dialog-folder-browser-dialog-input-box-and-message-box/.

    So the special chars `r and `n was captured.

    Wednesday, June 29, 2016 7:44 PM
  • Change:

      return $form.Tag

    to:

      return $textbox.Lines


    \_(ツ)_/

    • Marked as answer by JRLOPS Wednesday, June 29, 2016 8:39 PM
    Wednesday, June 29, 2016 8:13 PM
  • Change:

      return $form.Tag

    to:

      return $textbox.Lines


    \_(ツ)_/


    Thank you so much it works!!!
    Wednesday, June 29, 2016 8:40 PM