Solved

GAL/Powershell: Traverse within an OU and set customattribute = OU

Posted on 2013-01-08
6
823 Views
Last Modified: 2013-01-14
Experts -

Need some assistance hashing out this script. =)

We get a regular import of contacts from an external source. These mailcontacts are sorted by OU, and the entire GAL dump (OU and contacts) are controlled by the external source (they have a service account with us).

So what we get is a dump of contacts organized by OU in our AD structure - so the OU structure for this dump looks like:

boo.com
- External Contacts (root OU)
  - Dairy
    Head.Honcho@dairy.com
     - Milk
         John.Smith@milk.com
         Carrie.Underwood@milk.com
         Super.Man@cows.com
     - Cheeses
         Steve.Carrell@cheeses.com
       - Hard Cheese
           Whoopi.Goldberg@hardcheese.com
       - Soft Cheese
           John.Stamos@softcheese.com
           Jeff.Goldblum@softcheese.com
  - Meats
  john.hancock@meats.com
    - Red Meat
     bobby.flay@redmeat.com
     jennifer.gray@superdome.com

Contacts within the OUs do NOT always have the same email domain.

We need to make our own address lists to reflect the OU structure. What I was planning on doing was to make a powershell script to set the "customattribute1" field equal to their OU. We would then use the custom attribute field to create the appropriate address lists.

This Powershell command works by itself for 1 OU:

Get-Mailcontact -OrganizationalUnit "OU=Milk,OU=Dairy,OU=External Contacts,DC=boo,DC=com" -ResultSize Unlimited | Where {$_.CustomAttribute1 -eq "$null"} | Set-Mailcontact -CustomAttribute1 "milk"

Open in new window


However, I want to automate this a little bit more. Instead of having to run the same commands 50 times for 50 OUs, I'd like to create a powershell script to:
1) Traverse through the "External Contacts"
2) In each OU
3) Look at each mailcontact
4) If customattribute1 is blank, set customattribute1 for each contact equal to the OU
5) Move to next contact/OU

I would think this should be relatively simple, but I am still not familiar enough with PowerShell to figure this out.

THANK YOU!! =)
0
Comment
Question by:ThinkPaper
  • 3
  • 3
6 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 38756483
Try this..
Get-MailContact -ResultSize Unlimited -OrganizationalUnit "OU=External Contacts,DC=boo,DC=com" | % {
	If ($_.CustomAttribute1 -eq ""){
	 Set-Mailcontact  -Identity $_ -CustomAttribute1 $(($_.OrganizationalUnit).Split("/") | Select -Last 1)
	}
}

Open in new window

0
 
LVL 16

Author Comment

by:ThinkPaper
ID: 38759170
Unfortunately, it didnt look like it worked. While it did propogate through, it set all the customattribute value to the root OU versus the actual child OU. =/

I"ll keep playing with the script..
0
 
LVL 40

Expert Comment

by:Subsun
ID: 38759197
I have tested it in my lab and it's working for me.. It will set the CustomAttribute1 to the OU name where the contact object resides in. Is that what you looking for?
0
Being driven mad by email signature updates?

Having to make a change to your users’ email signatures, yet again? Feel like your head is going to explode? Rely on an Exclaimer email signature management solution to make the process simple!

 
LVL 16

Assisted Solution

by:ThinkPaper
ThinkPaper earned 0 total points
ID: 38759342
Ok. It looked like this worked!! It was missing a "ForEach" to grab and set the CustomAttribute field to the current child OU versus the root OU. Hope I was clear about that.

Get-Mailcontact -OrganizationalUnit "OU=Milk,OU=Dairy,OU=External Contacts,DC=boo,DC=com" -ResultSize Unlimited | Where {$_.CustomAttribute1 -eq "$null"} | ForEach { Set-Mailcontact  -Identity $_ -CustomAttribute1 $(($_.OrganizationalUnit).Split("/") | Select -Last 1)}

Open in new window

0
 
LVL 40

Expert Comment

by:Subsun
ID: 38760037
% is the alias for ForEach.. I am not sure why my script didn't work.. functionally both your script and my script does the same thing.. Any way glad you got it worked.. :-)
0
 
LVL 16

Author Closing Comment

by:ThinkPaper
ID: 38773796
Solution worked, but needed a little tweaking. Thank you!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
In this video we show how to create an Accepted Domain in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Mail Flow >> Ac…
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now