Need some PowerShell help

Normally, when I write a blog post, I'm trying to help other people out. I forget that it can work both ways. So, today's post is a plea for help: if you know a lot about PowerShell, I could use an answer to the following three questions. If you've got any insights, please drop me an email using the Contact link.


Question the First:

I've got a script that manipulates a user's delivcontlength property in Exchange 2003. This helps me manage the situation where I've got a few users who need to be able to receive 20MB messages while most everyone else only needs 10MB. My script grabs all of the user objects in the directory, iterates through the collection, checks to see if the user is one of the special users, and if not it sets the per-user limit to be 10MB.

$ds=New-Object DirectoryServices.DirectorySearcher
$ds.Filter="(&(objectcategory=person)(objectclass=user))"
$AllUsers=$ds.FindAll()
Foreach ($User in $AllUsers) {
  $oUser=$User.GetDirectoryEntry()
  if ($oUser.sAMAccountName -ne "deving") {
    $oUser.Put("delivcontlength", "10240")
    $oUser.SetInfo()
    $oUser.psbase.RefreshCache()
  }
  $oUser | select displayname,delivcontlength
}

This script does what I want to do, but what I don't know how to do is reset the delivcontlength attribute. If there's no per-user limit set, this attribute doesn't exist on the user object -- so how do I remove an attribute through PowerShell? Setting it to 0 doesn't work.

Edit: The correct answer is to use the PutEx method, which allows you to handle a collection of attributes, as well as delete existing attributes. I'll post an updated snippet of code next week that shows how to use PutEx in live code. Thanks to Andy Webb for the answer.


Question the Second:

Continuing with the delivcontlength attribute, when I go to check its value on a single user:

$oUser.Get(“delivcontlength”)

This throws an exception if the attribute isn’t set. How do I trap that exception inside of a script so I know that the property doesn’t exist, and can do something else based on that information?

Edit: The answer, again from Andy Web, is to use the GetEx method and specify the delivcontlength as one of the attributes in the collection. Then, check to see if a value is returned in the array.


Question the Third:

Given a variable with a DN, what is the easiest way to open the corresponding object using ADSI? I've tried the following with no success:

$DN = "LDAP://red-dc01:389/CN=Devin Ganger,OU=Users,OU=3Sharp Accounts,DC=redmond,DC=3sharp,DC=com"
$oUser = [ADSI]$DN

The point, of course, is to be able to retrieve the DNs from some other list such as a CSV and perform some operation on the listed objects by iterating through with a loop.

Edit: I'm told that this should in fact work. When I go paste it into a clean instance, it does, in fact work. Weird!

Print | posted on Thursday, March 29, 2007 12:38 PM

Comments on this post

# re: Need some PowerShell help

Requesting Gravatar...
Thank you
Left by kyle on Jul 10, 2007 12:40 AM
Comments have been closed on this topic.