| T O P I C R E V I E W |
| aval |
Posted - 05/27/2009 : 09:34:36 AM NOTE: script in second post works fine. If anyone wants to explain what I'd need to make the one in this post work, that's fine, and it might even be useful to someone, but I've got what I need from the second.
I want to import new contacts into Active Directory from a CSV file.
I want to do this at the Exchange Server using Powershell.
If I simply add a contact or distribution group to Active Directory, Exchange apparently does not automatically "see" it: I have to add it "again" in the EMC.
These are parents of students in an academic setting.
We want to have all parents added as contacts so teachers can contact them by email.
We have a separate .csv file for each grade. So I would edit each one as needed.
We also want them to be members of the appropriate distribution list (one per grade, so, in the US system: Pre-K, Kindergarten, Gr1, Gr2 [...] Gr11, Gr12.
If the script could add them to the appropriate distribution group as well, that would be great. But note the problem I mention after the script.
Anyway, here's what I came up with (I'll worry about the distro group later).
So what needs to be added / fixed for this to work?
################################################
$objOU=[ADSI]"LDAP://OU=Parents,DC=school,DC=org" $dataSource=import-csv "12thGr.csv" foreach($dataRecord in $dataSource) {
# Link variables to attribute in datasource
$givenName=$dataRecord.givenName $sn=$dataRecord.sn $displayName=$dataRecord.displayName $mail=$dataRecord.mail
#Create contact object
#NEXT LINE NEEDS CLARIFICATION - How do I create the CN and DN ?
$objContact=$objOU.Create("contact",CN=" + $given
$objContact.Put("givenName",$givenName) $objContact.Put("sn",$sn) $objContact.Put("displayName",$displayName) $objContact.Put("mail",$mail)
$objcontact.SetInfo() }
##############################################
Once again, the .csv files include parent contact information by grade.
Since parents could send more than once child to the school, there are duplicates (one entry for the parent in the 10thGr.csv file and another entry for the same parent in the 11thGr.csv file).
So, that parent will already exist as an object and cannot be recreated.
I can add them manually to a second or third distribution list as needed, but how can I prevent the script from stopping in this case - and create the remaining objects from the file?
-k ?
That might work for csvde. Not sure about Powershell. |
| 6 L A T E S T R E P L I E S (Newest First) |
| pdania |
Posted - 11/16/2011 : 08:22:33 AM I struggled with this for nearly a whole day, I don't understand Powershell and trying to learn it from the ground up which is what I'm doing is hard....very hard.
this link worked for me http://exchangestories.blogspot.com/2008/03/importing-mail-contacts-to-exchange.html
although I now need to put all these newly created mail contacts into an AD group. Good fun eh! :) |
| aznan |
Posted - 10/22/2010 : 11:37:32 AM Hi, i'm using the Import-Csv contacts.csv and am facing the same issue as you where we have parents whom have more than one child and since the object already exist, it doesnt allow for the same object to be re-created. I'm using the Import-Csv contacts.csv
Do you mind sharing the workaround?
Thanks in advance Aznan Nagor The International School of Penang (Uplands) www.uplands.org |
| Jazzy |
Posted - 05/28/2009 : 10:40:04 AM quote: Originally posted by aval
I placed this here: C:
Powershell found it.
Best practice is alway use an absolute path, a mistake is eassily made when another file with the same name exists in a folder in the PATH variable.
Current directory? then do ./myfile.csv.
quote: And the Powershell command in my second post (above) is more concise than whatever we're trying to do in the first post (Powershell trying to interact with ADSI/LDAP somehow, I think).
Maybe the first option make sense to somebody who has experience with scripting languages but I don't get it. And why would you, the built-in Exchange cmdlets are so powerful and even more when you combine them with import-csv like cmdlets. |
| aval |
Posted - 05/28/2009 : 08:56:57 AM quote: Did it help?
Yes, your explaination answers my questions.
Before reading this post, though, I simply tried the command with my data and much to my surprise, it worked on the first try!
quote: Import-Csv contacts.csv
I placed this here: C:
Powershell found it.
I needed to make some of the headings match, but once I did, as I said, everything worked perfectly.
And the Powershell command in my second post (above) is more concise than whatever we're trying to do in the first post (Powershell trying to interact with ADSI/LDAP somehow, I think). |
| Jazzy |
Posted - 05/27/2009 : 3:16:21 PM You're asking several things. First the New-MailContact cmdlet. You can read about the usage and the parameters in the documentation: http://technet.microsoft.com/en-us/library/bb124519.aspx (simply Google the cmdlet, then click the first hit)
Notice that the required parameters are on top of the list, followed by the optional parameters. According this table we need the Name and ExternalEmailAddress parameter to make it work. The minimal usage is like this:
New-MailContact -Name "Jetze Mellema" -ExternalEmailAddress stoppen@hotmail.com Agree? Now the csv-file magic, this is really interesting and not so complicated as it may seem at first.
The Import-csv cmdlet reads an csv-file en outputs the contents, which we can use as input for the next command. Lets create a csv-file with only this content:
FullName,Mail Jetze Mellema, stoppen@hotmail.com John Smith, mail@something.com
Save the file as test.csv. Now give the command 'import-csv test.csv', the output will be like:
FullName Mail -------- ----
Jetze Mellema stoppen@hotmail.com John Smith mail@something.com
As with all cmdlets you can pipe this output to other cmdlets as ft and fl. Notice that import-csv interprets the header of the file as the column names, we will use this names now to use the output as input for New-MailContact.
Import-csv test.csv | foreach { <insert our stuff here> }
Between the brackets we can write our cmdlet which will be run for every line in the csv-file. For example:
Import-csv test.csv | foreach { New-MailContact -Name $_.FullName -ExternalEmailAddress $_.Mail }
Notice we use $_ to refer to the data coming from the stream and then a dot and the name of the 'column'. I'm sure that other members on the forum can explain this in better terminology but this is all I have for you. 
Did it help? |
| aval |
Posted - 05/27/2009 : 11:38:35 AM I'm also looking at this, found at:
http://mymcp.blogspot.com/2007/07/bulk-importing-contacts-from-csv-to.html
################################################
Import-Csv contacts.csv | ForEach { New-MailContact -Name $_.displayName -Firstname $_.FirstName -LastName $_.LastName -ExternalEmailAddress $_.Emailaddress -OrganizationalUnit "YourDomain.Local/Contacts" }
#################################################
Trying to understand this:
Import-Csv = the PS command or operation that's going to import hte contacts.
Contacts.csv = the file name. Why not C:\contacts.csv, for example?
Does Powershell assume it's saved to the root? How would it find it otherwise?
| = pipeline, apparently.
{} = these enclose something - ???
New-MailContact = somehow this command is going to fetch the source info for the creation of new accounts in the .csv file. Not sure howhe's going to figure that out.
-Name = this will be the name of the contact, based on the displayName value in the .csv file.
$_.displayName = Somehow this means Powershell is to seek the displayName value in the csv. file.
Etc, etc.. Then:
-OrganizationalUnit YourDomain.Local/Contacts
This tells PS where to create the contacts?
Now, is there a script, that I could add to this one, or run separately, that would add these contacts to a designated distribution group?
|
|
|