Unable to find type [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 7 years and 6 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
User avatar
Technosapien
Posts: 9
Last visit: Wed Sep 21, 2016 4:56 am

Unable to find type [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]

Post by Technosapien »

I have a Catch block for the exception type that comes up when a user isn't found in AD.

In PowerShell Studio I'm still receiving the error Unable to find type [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException].

Same script works without issue in ISE. I have the Import-Module Active Directory at the top of my file. Can you advise what I've missed?
User.ps1 (107, 16): ERROR: At Line: 107 char: 16
ERROR: + ...      catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundEx ...
ERROR: +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: Unable to find type [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException].
ERROR:     + CategoryInfo          : ParserError: (:) [], ParseException
ERROR:     + FullyQualifiedErrorId : TypeNotFound
ERROR:
Product: PowerShell Studio 2015
Version: 4.2.89
Operating System: Windows 10 64-bit
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Unable to find type [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]

Post by jvierra »

This works on PS4 and later but may not on earlier versions.
  1. Try{
  2.     get-aduser noaccount -ea stop
  3. }
  4. Catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]{
  5.     Write-Host $_ -fore red
  6. }
User avatar
Technosapien
Posts: 9
Last visit: Wed Sep 21, 2016 4:56 am

Re: Unable to find type [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]

Post by Technosapien »

Hey jvierra.

I can confirm that in a basic setting such as follows the exception type can be detected.
try
{
	Get-ADUser -Identity 'FAKEACCOUNT'
}
catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]
{
	Write-Host "Account not detected exception"
}
However the exception type isn't recognised within my class (see pastebin[dot]com/Udb6z63D) by PowerShell Studio but it works without issue in PowerShell ISE.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Unable to find type [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]

Post by jvierra »

The class has some limitations. YO might want to report this to uservoice as a possible bug.

https://windowsserver.uservoice.com/for ... powershell
User avatar
Technosapien
Posts: 9
Last visit: Wed Sep 21, 2016 4:56 am

Re: Unable to find type [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]

Post by Technosapien »

@Jvierra

I think this is a PS Studio bug rather than a general PS one. This is evidenced by the type error not occurring when catch block is triggered in ISE.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Unable to find type [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]

Post by jvierra »

depends on whatcaused th eerror since you are ignoring most errors.

When we do a try/catch you MUST also apply a general catch block in case the exception is not parsed as expected.

YUO must also defer to the version of PowerShell. Versions before V5 did not honor many exception types. Look at the selection of PowerShell version you are using in PSS.

PSS uses the native PS engines. It is the engine that executes the code and not PSS.

Try/CAtch should look like this:
  1. Try{
  2.     Get-ADUser -Identity 'FAKEACCOUNT' -ErrorAction Stop
  3. }
  4. Catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]{
  5.     Write-Host "Account not detected exception"
  6. }
  7. Catch{
  8.      # catches all other exceptions.
  9. }
User avatar
Technosapien
Posts: 9
Last visit: Wed Sep 21, 2016 4:56 am

Re: Unable to find type [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]

Post by Technosapien »

depends on whatcaused th eerror since you are ignoring most errors.

When we do a try/catch you MUST also apply a general catch block in case the exception is not parsed as expected.
I understand this. Because I can only trigger 2 error types for this query, I am catching them. I will later add a catch to email me any other unexpected errors.
YUO must also defer to the version of PowerShell. Versions before V5 did not honor many exception types. Look at the selection of PowerShell version you are using in PSS.
I am using PowerShell V5 - 64 bit
PSS uses the native PS engines. It is the engine that executes the code and not PSS.
I'm not sure what to say then. I can reproduce the issue in PSS but when I open the same file in PSISE it works without issue.
Can you please review the attached file to demonstrate? When run in PSISE it runs without issue. With PSS 2015 4.2.89 I receive this error.
>> Debugging (UserTest.ps1) Script...
>> Platform: V5 64Bit (STA)
UserTest.ps1 (147, 10): ERROR: At Line: 147 char: 10
ERROR: + ...      catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundEx ...
ERROR: +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: Unable to find type [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException].
ERROR:
UserTest.ps1 (184, 10): ERROR: At Line: 184 char: 10
ERROR: +         catch [Microsoft.ActiveDirectory.Management.ADException]
ERROR: +                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: Unable to find type [Microsoft.ActiveDirectory.Management.ADException].
ERROR:     + CategoryInfo          : ParserError: (:) [], ParseException
ERROR:     + FullyQualifiedErrorId : TypeNotFound
Attachments
UserTest.ps1
(10.55 KiB) Downloaded 302 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Unable to find type [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]

Post by jvierra »

I don't have PSS installed on a domain controller so I cannot test your code under PSS.

You are not running the code native but under the Debugger so that may create an issue. Does the code run correctly without using the debugger?

Why use AD CmdLets to create a class for user management. It would be easier to use ADSI as it is available on all platforms and the exceptions are easier to manage.
User avatar
ISAssets
Posts: 10
Last visit: Wed Mar 27, 2024 6:25 pm

Re: Unable to find type [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]

Post by ISAssets »

jvierra wrote:I don't have PSS installed on a domain controller so I cannot test your code under PSS.
Doesn't need to be on a Domain Controller. Just run on a PC with AD Users and Computers applet.
jvierra wrote:You are not running the code native but under the Debugger so that may create an issue. Does the code run correctly without using the debugger?
Fails to run in normal or Debug mode.
>> Platform: V5 64Bit (STA)
Globals.ps1 (185, 10): ERROR: At Line: 185 char: 10
ERROR: +             catch [Microsoft.ActiveDirectory.Management.ADException]
ERROR: +                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: Unable to find type [Microsoft.ActiveDirectory.Management.ADException].
ERROR:     + CategoryInfo          : ParserError: (:) [], ParseException
ERROR:     + FullyQualifiedErrorId : TypeNotFound
ERROR:
>> Script Ended
jvierra wrote:Why use AD CmdLets to create a class for user management. It would be easier to use ADSI as it is available on all platforms and the exceptions are easier to manage.
I'm hoping to rewrite this with ADSI at a later time. Want to improve my knowledge first. Possibly make in C#. For now though my team who I'm writing this for need a tool.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Unable to find type [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]

Post by jvierra »

There is no AD US and computers applet. Do you mean RSAT?

I do not have that set up. I use ADSI from non DCs mostly.

Active Directory can only be used with RSAT installed.
This topic is 7 years and 6 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