2012-05-25 63 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

我們正在解決這個問題,問題不會解決。雖然設計不是 正確...應用程序可能已經寫入了期待 - 並且更改 可能會破壞這些應用程序。解決方法是捕獲異常 - 不是 漂亮,同意,但沒有足夠的理由來解決問題。 文檔需要修復,我將打開一個文檔工作項目 這個。

相關問題