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

Ask your Windows 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.
User avatar
Technosapien
Posts: 9
Joined: Wed Jul 22, 2015 4:37 pm

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

Post by Technosapien » Wed Sep 14, 2016 12:48 am

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

User avatar
jvierra
Posts: 13790
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Wed Sep 14, 2016 2:58 am

This works on PS4 and later but may not on earlier versions.

Code: Select all

Try{
get-aduser noaccount -ea stop
}
Catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]{
Write-Host $_ -fore red
}

User avatar
Technosapien
Posts: 9
Joined: Wed Jul 22, 2015 4:37 pm

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

Post by Technosapien » Wed Sep 14, 2016 5:24 pm

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.

User avatar
jvierra
Posts: 13790
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Thu Sep 15, 2016 6:11 am

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
Joined: Wed Jul 22, 2015 4:37 pm

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

Post by Technosapien » Thu Sep 15, 2016 4:48 pm

@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.

User avatar
jvierra
Posts: 13790
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Thu Sep 15, 2016 5:19 pm

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:

Code: Select all

Try{
Get-ADUser -Identity 'FAKEACCOUNT' -ErrorAction Stop
}
Catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]{
Write-Host "Account not detected exception"
}
Catch{
# catches all other exceptions.
}

User avatar
Technosapien
Posts: 9
Joined: Wed Jul 22, 2015 4:37 pm

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

Post by Technosapien » Sun Sep 18, 2016 4:40 am

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 127 times

User avatar
jvierra
Posts: 13790
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Sun Sep 18, 2016 8:24 am

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
Joined: Tue Jul 21, 2015 6:50 pm

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

Post by ISAssets » Sun Sep 18, 2016 5:21 pm

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.

User avatar
jvierra
Posts: 13790
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Sun Sep 18, 2016 6:41 pm

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.

Locked