Create New Object DirectoryEntry and call Static Exists Method

1
$Credential = Get-Credential  
[System.DirectoryServices.DirectoryEntry]$dm = new-object System.DirectoryServices.DirectoryEntry(   
    "LDAP://DC1/DC=MyDom,DC=COM",   
    $($Credential.UserName),  
    $($Credential.GetNetworkCredential().password ),   
    [System.DirectoryServices.AuthenticationTypes]::Secure  
    )  
$dm::Exists("LDAP://OU=TestOU,DC=MyDom,DC=COM")  

I'm trying to use the static Exists method, but when I do like the above I receive:

"Unable to cast object of type 'System.DirectoryServices.DirectoryEntry' to type 'System.Type'."

If I change to: $dm.Exists("LDAP://OU=TestOU,DC=MyDom,DC=COM")

I recieve:

"Exception calling "exists" with "1" argument(s): "Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))"

Even tried: $dm::Exists("LDAP://DC1/OU=TestOU,DC=MyDom,DC=COM") with same error result.

If I specify the type and call Exists "[System.DirectoryServices.DirectoryEntry]::Exists("LDAP://DC1/OU=TestOU,DC=MyDom,DC=COM"" while logged on with domain account, no problems..

I guess the real question is, how can I call a static method with an object variable when set to a specific type? Or do I need to create a custom type?

powershell-3.0
directoryservices
asked on Stack Overflow Jul 3, 2014 by JKStinn

1 Answer

0

Though did not understand your problem compaletey as in what actually you want to achieve but below is actually the way you can call static method
[System.DirectoryServices.DirectoryEntry]::Exists($dm.Path)

Please take a look at this also http://technet.microsoft.com/en-us/library/dd347632.aspx

answered on Stack Overflow Jul 18, 2014 by Shagun Sharma Tamta

User contributions licensed under CC BY-SA 3.0