2012-05-25 1 views
4

J'essaie de créer un utilisateur en utilisant l'objet ADSI s'il n'existe pas déjà. Voici les résultats étranges que j'obtiens[ADSI] :: Exists lève une exception au lieu de renvoyer 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" 

La dernière ligne n'a aucun sens pour moi. Pourquoi lancer une exception quand je spécifie que je cherche spécifiquement un utilisateur, mais quand je ne spécifie pas que je veux un utilisateur cela fonctionne très bien? Cela me semble complètement inintéressant. Qu'est-ce que je rate?

Répondre

5

Son un bug et Microsoft ne réparer -

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

Nous résolvons ce bug ne réglera pas. Bien que la conception n'est pas droite ... Des applications ont peut-être été écrites en attente de cela - et le changement pourrait casser ces applications. La solution de contournement consiste à attraper l'exception - pas jolie, d'accord, mais pas assez de justification pour un correctif. La documentation doit être corrigée et j'ouvrirai un document de travail pour ceci.

Questions connexes