-ErrorVariable only grabbing first error

This topic contains 4 replies, has 3 voices, and was last updated by  camelCreed 3 months, 3 weeks ago.

  • Author
    Posts
  • #71497

    camelCreed
    Participant

    Hello!

    I am using Try/Catch with New-ADUser. I am intentionally breaking the add by giving a bad syntax for the -Path argument.

    I have three objects being input to my script. They all three error. $error.count shows 3 errors when I run this script.

    However, my error variable, which is set up like, -ErrorVariable ADFail, only shows the last occurrence instead of all three.

    When I run $ADFail, I see a perfectly caught exception, but only for the last failure.

    How can I get $ADFail to show all instances??

    $newStaff | ForEach-Object {
    
    Try { 
    
        if ($_.ElemOrSecOU -ne "NULL") {
    
            $path = "OU=$($_.LocationOU),OU=$($_.ElemOrSecOU),OU=$($_.CertOU),OU=User Accounts,DC=domain,DC=my"
    
        } Else {
    
            $path = "OU=$($_.abbrOtherOU),OU=$($_.CertOU),OU=User Accounts,DC=domain,DC=my" }
    
        New-ADUser  -Name ($_.FirstName + ' ' + $_.LastName) `
                    -Surname $_.LastName `
                    -DisplayName $_.DisplayName `
                    -GivenName $_.FirstName `
                    -EmailAddress $_.EmailAddress `
                    -SamAccountName $_.UserLogonName `
                    -UserPrincipalName ($_.UserLogonName + '@my.domain') `
                    -Title ($_.WorkDuties) `
                    -HomeDrive "H:" `
                    -HomeDirectory ($_.HomeServer + '\HOME\' + $_.UserLogonName) `
                    -Path $path `
                    -AccountPassword (ConvertTo-SecureString "mypass" -AsPlainText -force) `
                    -ChangePasswordAtLogon $true `
                    -Enabled $true `  -ErrorVariable ADFail
                    
        } Catch [Microsoft.ActiveDirectory.Management.ADException] {$ADFail} 
    
        $AccountAddFail = $_.UserLogonName + ' ' + $ADFail
    
    }

    $ADFail looks like this...

    PS C:\Windows\system32> $AccountAddFail
    USERNAME1 System.Management.Automation.CmdletInvocationException: The object name has bad syntax —> Microsoft.ActiveDirectory.Management.ADException: The object name has bad syntax —> System.ServiceModel.FaultException: Active Directory returned an error processing the operation.
    — End of inner exception stack trace —

    It should be...

    USERNAME1 error
    USERNAME2 error
    USERNAME3 error

    ...right??

    Any advice appreciated. Thank you!

  • #71498

    David Schmidtberger
    Participant

    think through the logic..

    $AccountAddFail = $_.UserLogonName + ' ' + $ADFail
    runs each time through the foreach loop where an error occurs

    you are overwriting $AccountAddFail each time an error occurs.
    you can try something like

    $AccountAddFail = $AccountAddFail + $_.UserLogonName + ' ' + $ADFail
    

    that probably won't look pretty and you'll probably want to insert some carriage returns.. but that will allow you to get all of the errors in the foreach loop

    this will introduce another complexity in that if you run this multiple times in a single session your variable will always contain all of the errors, so in the beginning of your script you probably want to do something like $AccountAddFail = ""

    • #71501

      Chris Bakker
      Participant

      You just have to add the info to the variable, not setting it (basicly overwrites it)

      $AccountAddFail += $_.UserLogonName + ' ' + $ADFail

      Adding "`r" gives you a new line

    • #71506

      camelCreed
      Participant

      Thank you. I will work on this and update when I make some progress.

    • #76744

      camelCreed
      Participant

      Checking in to thank you again for you help, and to say that this looks so silly...but it was early days with ps for me. I have learned a lot and completely understand throwing exceptions from each iteration in a predefined array by using += in the catch.

      Thanks again!

You must be logged in to reply to this topic.