[ADSI] :: Exists引发异常,而不返回False [英] [ADSI]::Exists throws an exception instead of returning False
问题描述
我正在尝试使用ADSI对象创建用户(如果尚不存在)。这是我得到的奇怪结果
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?
推荐答案
它是一个错误,Microsoft不会修复-
Its a bug and Microsoft will not fix it --
我们正在解决此错误,因为它无法修复。尽管设计不是
正确的……但应用程序可能是期望如此编写的,而更改
可能会破坏这些应用程序。解决方法是捕获异常-不是
漂亮,同意,但没有足够的理由进行修复。
文档需要修复,我将为此
打开一个文档工作项。
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.
这篇关于[ADSI] :: Exists引发异常,而不返回False的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!