【问题标题】:[ADSI]::Exists throws an exception instead of returning False[ADSI]::Exists 抛出异常而不是返回 False
【发布时间】:2012-05-25 15:35:54
【问题描述】:

如果 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"

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

【问题讨论】:

    标签: powershell adsi


    【解决方案1】:

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

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

    我们正在解决此错误,因为无法修复。虽然设计不是 对... 可能已经编写了应用程序期待这一点 - 以及变化 可能会破坏这些应用程序。解决方法是捕获异常 - 不是 漂亮,同意,但没有足够的理由进行修复。这 文档需要修复,我将打开一个文档工作项 这个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-08
      相关资源
      最近更新 更多