[ADSI] :: Exists throws an exception instead of returning False

I am trying to create a user using an ADSI object if it does not already exist. Here are the weird results I get

#Check a user that I know exists [ADSI]::Exists("WinNT://localhost/micah,user") #True #Check a group that I know exists [ADSI]::Exists("WinNT://localhost/administrators,group") #True #Check a group that DOESN'T exist [ADSI]::Exists("WinNT://localhost/whoops,group") #False #Check a user that DOESN'T exist (NOT specifying that the obect is a user) [ADSI]::Exists("WinNT://localhost/test") #False (This works fine) #Check a user that DOESN'T exist (specifying that the obect IS a user) [ADSI]::Exists("WinNT://localhost/test,user") #Throws exception "The user name could not be found" 

The last line makes no sense to me. Why throw an exception when I indicate that I am specifically looking for a user, but when I DONT indicate that I want the user to work fine? It seems completely unintuitive to me. What am I missing?

+4
source share
1 answer

His mistake and Microsoft will not fix it -

http://connect.microsoft.com/VisualStudio/feedback/details/337682/directoryentry-exists-throws-exception-for-non-existent-winnt-object

We resolve this error, as we will not correct it. Although the design is not right ... The applications may have been written expecting this - and a change could break these applications. The workaround is to catch the exception - not quite agreed, but not enough excuse to fix it. The documentation should be fixed, and I will open a working document for this.

+5
source

Source: https://habr.com/ru/post/1414403/


All Articles