2012-05-25 67 views
4

我想创建一个用户使用ADSI对象,如果它不存在。这里是我得到的奇怪结果[ADSI] ::存在抛出一个异常,而不是返回False

#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" 

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

回答

5

它的一个错误,微软将不会解决它 -

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

我们正在解决这个问题,问题不会解决。虽然设计不是 正确...应用程序可能已经写入了期待 - 并且更改 可能会破坏这些应用程序。解决方法是捕获异常 - 不是 漂亮,同意,但没有足够的理由来解决问题。 文档需要修复,我将打开一个文档工作项目 这个。

相关问题