[ADSI] :: Exists抛出异常而不是返回False

Mic*_*cah 5 powershell adsi

我正在尝试使用ADSI对象创建用户(如果它尚不存在).以下是我得到的奇怪结果

#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"
Run Code Online (Sandbox Code Playgroud)

最后一行对我没有意义.当我指定我专门寻找用户时为什么会抛出异常,但是当我不指定我想要一个用户时它工作得很好?这对我来说似乎完全不直观.我错过了什么?

And*_*ndi 6

它是一个错误,微软不会修复它 -

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

我们正在解决这个错误,因为无法修复.虽然设计不对......应用程序可能已经写好了 - 并且这些更改可能会破坏这些应用程序.解决方法是捕获异常 - 不是很好,同意,但没有足够的理由来修复.需要修复文档,我将为此打开文档工作项.