Keep distribution groups synced with .csv-files

by PatricF at 2013-01-14 07:50:16


I have three distribution groups and three .csv-files. I need to “sync” these daily. I’ve tried to make a simple script that keeps this up-to-date but I ran into problems on the way.
So, I just wanted to see what methods you would use/recommend for doing this.

This far I’ve done this:

[code2=powershell]Import-Csv C:\temp\first.csv | ForEach-Object {

$checkcontact=Get-MailContact -Identity “$name”

if(!$checkcontact) {
“Here I create the mailcontact and add it to the group if it doesn’t exists.”
} else {
$checkmember=Get-DistributionGroupMember -Identity GroupOne | where {$.name -Like “$name”}
if(!$checkmember) {
“Here I add the member to the group if the member already exists as a mailcontact and is not in the specific group.”

I do this for each .csv-file, also this method is really slow and I think there might be a more efficient way of doing this.
Then there’s the problem of removing contacts that’s not in a group anymore and remove a user from a group if it’s no longer member of it but a member of another group.

I would want to do something like this, but I have no frickin idea where to start:

For each object in csv-file/files {
check if the user exists, if not create as mailcontact.
check which group the user should/shouldn’t be in, add/remove from groups.
if user’s not in a group anymore, remove him as mailcontact.

As I mentioned I have three groups and three csv-files. One user can be in one or more groups and can change group too.
Is there a less complicated and smoother way of doing this?

by PatricF at 2013-01-15 05:46:13
I made a two ForEach-loops, one that checked if all the users on the Exchange where in the .csv-file and then one where I checked if the users in the csv-file where on the Exchange and created them if necessary.
I also use CustomAttribues that I add/remove when a user joins/leaves a group. If a user has $null on all of them he gets removed.
by ArtB0514 at 2013-01-15 06:33:45
It looks like your $name object isn’t what you think it is. Try this:
Import-Csv C:\temp\first.csv | Select -First 1 | ForEach-Object {
@ “
`$name contains $($name.Count) objects: