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

↘锁芯ラ 提交于 2019-12-07 05:02:22

问题


I'm trying to create a user using the ADSI object if it doesn't already exist. Here are the strange results I'm getting

#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 would it throw an exception when I specify that I'm specifically looking for a user, but when I DONT specify that I want a user it works just fine? This seems completely unintuitive to me. What am I missing?


回答1:


Its a bug and Microsoft will not fix it --

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

We are resolving this bug as won't fix. Although the design isn't right... Apps might have been written expecting this - and the change might break those apps. The workaround is to catch the exception - not pretty, agreed, but not enough justification for a fix. The documentation needs to be fixed and I will open a doc workitem for this.



来源:https://stackoverflow.com/questions/10757316/adsiexists-throws-an-exception-instead-of-returning-false

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!