Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 1 year and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
Dominic.Lanthier
Posts: 1
Last visit: Sat Jan 28, 2023 9:32 am

Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException

Post by Dominic.Lanthier »

Hi !

I am coding something using ActiveDirectory commandlet Get-AdUser.

$tempUser = Get-ADUser -Identity "Tataboutlamine" -Property * -ErrorAction Stop

If the user's identity cannot be found in AD, the reference tells us that the Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException will be raised. When I try to try-->catch the specific exception, PowerShell Studio 2022 reports that :

At line:590 char:10 Type [ActiveDirectory.Management.ADIdentityNotFoundException] introuvable.

Here a code sample :
  1. try
  2.         {
  3.             $tempUser = Get-ADUser -Identity $strUserName -Property * -ErrorAction Stop
  4.             $this.setAddress(($tempUser).StreetAddress)
  5.             $this.setCity(($tempUser).L)
  6.             $this.setPostalCode(($tempUser).PostalCode)
  7.             $this.setProvince(($tempUser).st)
  8.             $this.setCountry(($tempUser).Co)
  9.             $this.setOffice(($tempUser).physicalDeliveryOfficeName)
  10.             $this.setDepartment(($tempUser).Department)
  11.             $this.setEmployeeType(($tempUser).EmployeeType)
  12.             $this.setDescription(($tempUser).Description)
  13.             $this.setOfficePhone(($tempUser).TelephoneNumber)
  14.             $this.setCompany(($tempUser).Company)
  15.             $this.setTitle(($tempUser).Title)
  16.                        
  17.             $tempUser = $null
  18.         }
  19.         Catch [ActiveDirectory.Management.ADIdentityNotFoundException]
  20.         {
  21.             throw [ADUserAccountException]::new("$(GetText('msgSourceAccountNotFound'))", $strUserName)
  22.         }
Is there a Module, Namespace or something like that that I need to add to the project?
It is working when I submit this code into Powershell ISE.

Thanks !
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException

Post by jvierra »

That exception is not "catchable".

The correct way to return a user object by identity is this:
  1. if ($user = Get-Aduser -Filter "SamAccountName -eq '$userid'" ){
  2.      #user was found.
  3. }else{
  4.     # user not found
  5. }
This topic is 1 year and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked