I want my DirectoryEntry methods back

RC2 is out and we have a new type alias [ADSI] for dealing with DirectoryEntry objects in AD, but inexplicably (at least to me), we’ve lost all the methods associated with that class unless you change everything to use psbase

So instead of saying something like

PS>$Entry = New-Object DirectoryServices.DirectoryEntry (“LDAP://dc=domain,dc=com”)

I can say

PS>$Entry = [ADSI] “LDAP://dc=domain,dc=com”

But instead of saying

PS>$Entry.get_Children()

I have to say

PS>$Entry.psbase.get_Children() 

And a fair amount of working code no longer works.

I don’t pretend to understand the discussion on the newsgroup about “adapting” and why using psbase is better than before. So in the spirit of PowerShell, I’ve just extended the type definition for DirectoryEntry so that I can still use the previous code.

For each of the previous methods for DirectoryEntry, I’ve added a ScriptMethod. So for example,

<ScriptMethod>
  <Name>get_Children</Name>
  <Script>
    $this.psbase.get_Children()
  </Script>
</ScriptMethod>

allows me to continue to use

$Entry.get_Children()

without the psbase “layer”.

This also allows previous code like MoW’s

<ScriptProperty>
  <Name>PasswordLastChanged</Name>
  <GetScriptBlock>
  $this.InvokeGet(‘PasswordLastChanged’)
  </GetScriptBlock>
</ScriptProperty>

to work again. I haven’t tested these definitions thoroughly so YMMV, but if you’d like to try them out, please help yourself to a copy of them. Comments are always welcome also.

Advertisements

One response to “I want my DirectoryEntry methods back

  1. justanothersysadmin

    Wow,

    I had not really gotten into ScriptMethods, but your last few posts have really shown their usefulness. The whole “psbase” is also a bit over my head (although I have seen some posts from the PS development guys that they might have made a mistake in releasin V1 that way), so this is definitely a neat trick around it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s