cawasaki
asked on
Pwershell script to import external contact in Exchange 2007-AD
Hello,
I need a powershell script to import in my AD-Exchange 2007 an external contact.
i have a csv file in this form:
sn,givenName,physicalDeliv eryOfficeN ame,mail
SMITH,John,Atlanta,john.sm ith@domain eA.com
HIRTEN,Christina,France,ch ristina.hi rten@domai neb.com
...........
the script must:
1-use the csv file to create an external contact in a specific OU
2-The display name must be in this form: SMITH John (Atlanta) = sn givenname (physicalDeliveryOfficeNam e)
3- if i lunsh the script and the contact is already exist, the script must ignore it and continue the creation of external contact
4-after the execution of script, i need in the screen the number of added external contact and the number of ignored creation caused bye the existence of the external contact.
I am in Exchange 2007 SP3 CCR CLUSTER and AD in Windows 2008 SP2.
Very thanks if you can help me.
I need a powershell script to import in my AD-Exchange 2007 an external contact.
i have a csv file in this form:
sn,givenName,physicalDeliv
SMITH,John,Atlanta,john.sm
HIRTEN,Christina,France,ch
...........
the script must:
1-use the csv file to create an external contact in a specific OU
2-The display name must be in this form: SMITH John (Atlanta) = sn givenname (physicalDeliveryOfficeNam
3- if i lunsh the script and the contact is already exist, the script must ignore it and continue the creation of external contact
4-after the execution of script, i need in the screen the number of added external contact and the number of ignored creation caused bye the existence of the external contact.
I am in Exchange 2007 SP3 CCR CLUSTER and AD in Windows 2008 SP2.
Very thanks if you can help me.
Hi there,
Perhaps like this.
Chris
Perhaps like this.
$OU = "domain.com/some OU"
$AlreadyExist = 0
$Created = 0
Import-Csv "TheFile.csv" | ForEach-Object {
If (Get-MailContact $_.mail) {
$AlreadyExist++
} Else {
$Name = "$($_.sn) $($_.givenName) ($($_.physicalDeliveryOfficeName))"
New-MailContact -Name $Name -ExternalEmailAddress $_.mail -DisplayName $Name `
-FirstName $_.givenName -LastName $_.sn -OrganizationalUnit $OU
$Created++
}
}
Write-Host "Created $Created contacts. $AlreadyExist already exist."
Cheers,Chris
ASKER
Hi Chris,
I test :)
I test :)
ASKER
not work:
[PS] C:\Temp\PLOP>.\PLOP.ps1
WARNING: By default only the first 1000 items are returned. To change the number of items returned, specify the parameter
"-ResultSize". To return all items specify "-ResultSize Unlimited" (Note: Returning all items may take a very long time
and consume a large amount of memory depending on the actual number of items). It is not recommended to store the results
in a variable; instead pipe the results to another task or script to perform batch changes.
WARNING: By default only the first 1000 items are returned. To change the number of items returned, specify the parameter
"-ResultSize". To return all items specify "-ResultSize Unlimited" (Note: Returning all items may take a very long time
and consume a large amount of memory depending on the actual number of items). It is not recommended to store the results
in a variable; instead pipe the results to another task or script to perform batch changes.
Created 0 contacts. 2 already exist.
ASKER
and it say 2 already exist but not exist!
The Mail field is blank for some of the entries in the CSV file? Should be the only reason it tries to find more than 1000.
Modified to capture those.
Modified to capture those.
$OU = "domain.com/some OU"
$AlreadyExist = 0
$MailIsBlank = 0
$Created = 0
Import-Csv "TheFile.csv" | ForEach-Object {
If (!$_.mail) {
$MailIsBlank++
} Else {
If (Get-MailContact $_.mail) {
$AlreadyExist++
} Else {
$Name = "$($_.sn) $($_.givenName) ($($_.physicalDeliveryOfficeName))"
New-MailContact -Name $Name -ExternalEmailAddress $_.mail -DisplayName $Name `
-FirstName $_.givenName -LastName $_.sn -OrganizationalUnit $OU
$Created++
}
}
}
Write-Host "Created $Created contacts. $AlreadyExist already exist. Mail wasn't filled in for $MailIsBlank."
Chris
ASKER
ok, the problem is in my csv file, i have a " ;" separator and not ",". I have change that and the first one script is ok but with small problem:
1-i need to enter a physicalDeliveryOfficeName value in AD attribute account
2-we have a problem with alias, exemple:
Name Alias
---- -----
SMITH John (Atlanta) SMITHJohn?Atlanta?
the alias must be in this form: "john.smith" in lower case letter.
thanks Chris
1-i need to enter a physicalDeliveryOfficeName
2-we have a problem with alias, exemple:
Name Alias
---- -----
SMITH John (Atlanta) SMITHJohn?Atlanta?
the alias must be in this form: "john.smith" in lower case letter.
thanks Chris
Righto :)
Chris
$OU = "domain.com/some OU"
$AlreadyExist = 0
$MailIsBlank = 0
$Created = 0
Import-Csv "TheFile.csv" -Delimiter ";" | ForEach-Object {
If (!$_.mail) {
$MailIsBlank++
} Else {
If (Get-MailContact $_.mail) {
$AlreadyExist++
} Else {
$Name = "$($_.sn) $($_.givenName) ($($_.physicalDeliveryOfficeName))"
$Alias = "$($_.givenName).$($_.sn)".ToLower()
$Contact = New-MailContact -Name $Name -ExternalEmailAddress $_.mail -DisplayName $Name `
-Alias $Alias -FirstName $_.givenName -LastName $_.sn -OrganizationalUnit $OU
Set-Contact $Contact.DistinguishedName -Office $_.physicalDeliveryOfficeName
$Created++
}
}
}
Write-Host "Created $Created contacts. $AlreadyExist already exist. Mail wasn't filled in for $MailIsBlank."
Cheers,Chris
ASKER
error :
[PS] C:\Temp\PLOP>.\PLOP.ps1
Import-Csv : A parameter cannot be found that matches parameter name 'Delimiter'.
At C:\Temp\PLOP\PLOP.ps1:7 char:36
+ Import-Csv "TheFile.csv" -Delimiter <<<< ";" | ForEach-Object {
Created 0 contacts. 0 already exist. Mail wasn't filled in for 0.
[PS] C:\Temp\PLOP>.\PLOP.ps1
Import-Csv : A parameter cannot be found that matches parameter name 'Delimiter'.
At C:\Temp\PLOP\PLOP.ps1:7 char:36
+ Import-Csv "TheFile.csv" -Delimiter <<<< ";" | ForEach-Object {
Created 0 contacts. 0 already exist. Mail wasn't filled in for 0.
ASKER
ok, i have delete the "-Delimiter ";"" from the script and work better.
ASKER
I have this error when i add a contact, the contact is added, but the error persist! :
Contact : The operation could not be performed because object 'jhon.smith@domain.com' could not be found on domain controller 'server01.domaine.local'.
mp\PLOP\PLOP.ps1:11 char:24
(Get-MailContact <<<< $_.mail) {
Contact : The operation could not be performed because object 'jhon.smith@domain.com' could not be found on domain controller 'server01.domaine.local'.
mp\PLOP\PLOP.ps1:11 char:24
(Get-MailContact <<<< $_.mail) {
ASKER
Chris?
The error is simply because the account cannot be found, which is normal considering you are only just now creating it. To prevent the error from popping up add the following line to the beginning of your script:
$ErrorActionPreference = "SilentlyContinue"
$ErrorActionPreference = "SilentlyContinue"
ASKER
hum thanks Gus i will try it.
Now i have another problem :)
if a value is empty like sn or given name, the contact is not created. its possible to change the script to ignore empty value like in this exemple:
sn,givenName,physicalDeliv eryOfficeN ame,mail
SMITH,John,john.smith@doma ineA.com
HIRTEN,,France,christina.h irten@doma ineb.com
the first exemple, the physicaldelivryofficename is empty
the second, the givenname is empty
thanks for your help
Now i have another problem :)
if a value is empty like sn or given name, the contact is not created. its possible to change the script to ignore empty value like in this exemple:
sn,givenName,physicalDeliv
SMITH,John,john.smith@doma
HIRTEN,,France,christina.h
the first exemple, the physicaldelivryofficename is empty
the second, the givenname is empty
thanks for your help
Adding to Chris' code, try the following:
$OU = "domain.com/some OU"
$ErrorActionPreference = "SilentlyContinue"
$AlreadyExist = 0
$MailIsBlank = 0
$Created = 0
Import-Csv "TheFile.csv" | ForEach-Object {
If (!$_.mail)
{
$MailIsBlank++
}
Else
{
If (Get-MailContact $_.mail)
{
$AlreadyExist++
}
Else
{
if (!$_.sn)
{ $sn = $null }
Else
{ $sn = $_.sn }
if (!$_.givenName)
{ $givenName = $null }
Else
{ $givenName = $_.givenName }
if (!$_.physicalDeliveryOfficeName)
{ $PDON = $null }
Else
{ $PDON = $_.physicalDeliveryOfficeName }
$Name = "$($sn) $($givenName) ($($physicalDeliveryOfficeName))"
$Alias = "$($_.givenName).$($_.sn)".ToLower()
$Contact = New-MailContact -Name $Name -ExternalEmailAddress $_.mail -DisplayName $Name -Alias $Alias -FirstName $_.givenName -LastName $_.sn -OrganizationalUnit $OU
Set-Contact $Contact.DistinguishedName -Office $_.physicalDeliveryOfficeName
$Created++
}
}
}
Write-Host "Created $Created contacts. $AlreadyExist already exist. Mail wasn't filled in for $MailIsBlank."
ASKER
hi GusGallows,
sorry, your script version not work.
if i test with a contact without sn or givenname or office it bot work
if i try with a complete contact information, it work but the displayname is not correctly set, i have not the office in the ()!
thnaks
sorry, your script version not work.
if i test with a contact without sn or givenname or office it bot work
if i try with a complete contact information, it work but the displayname is not correctly set, i have not the office in the ()!
thnaks
Yeah, sorry, it was a typo on my part. Try it this way:
$OU = "domain.com/some OU"
$ErrorActionPreference = "SilentlyContinue"
$AlreadyExist = 0
$MailIsBlank = 0
$Created = 0
Import-Csv "TheFile.csv" | ForEach-Object {
If (!$_.mail)
{
$MailIsBlank++
}
Else
{
If (Get-MailContact $_.mail)
{
$AlreadyExist++
}
Else
{
if (!$_.sn)
{ $sn = $null }
Else
{ $sn = $_.sn }
if (!$_.givenName)
{ $givenName = $null }
Else
{ $givenName = $_.givenName }
if (!$_.physicalDeliveryOfficeName)
{ $PDON = $null }
Else
{ $PDON = $_.physicalDeliveryOfficeName }
$Name = "$($sn) $($givenName) ($($PDON)"
$Alias = "$($_.givenName).$($_.sn)".ToLower()
$Contact = New-MailContact -Name $Name -ExternalEmailAddress $_.mail -DisplayName $Name -Alias $Alias -FirstName $givenName -LastName $sn -OrganizationalUnit $OU
Set-Contact $Contact.DistinguishedName -Office $PDON
$Created++
}
}
}
Write-Host "Created $Created contacts. $AlreadyExist already exist. Mail wasn't filled in for $MailIsBlank."
ASKER
sorry, not work
Gah, missing the trailing ). I must be too tired. Try this:
$OU = "domain.com/some OU"
$ErrorActionPreference = "SilentlyContinue"
$AlreadyExist = 0
$MailIsBlank = 0
$Created = 0
Import-Csv "TheFile.csv" | ForEach-Object {
If (!$_.mail)
{
$MailIsBlank++
}
Else
{
If (Get-MailContact $_.mail)
{
$AlreadyExist++
}
Else
{
if (!$_.sn)
{ $sn = $null }
Else
{ $sn = $_.sn }
if (!$_.givenName)
{ $givenName = $null }
Else
{ $givenName = $_.givenName }
if (!$_.physicalDeliveryOfficeName)
{ $PDON = $null }
Else
{ $PDON = $_.physicalDeliveryOfficeName }
$Name = "$($sn) $($givenName) ($($PDON))"
$Alias = "$($_.givenName).$($_.sn)".ToLower()
$Contact = New-MailContact -Name $Name -ExternalEmailAddress $_.mail -DisplayName $Name -Alias $Alias -FirstName $givenName -LastName $sn -OrganizationalUnit $OU
Set-Contact $Contact.DistinguishedName -Office $PDON
$Created++
}
}
}
Write-Host "Created $Created contacts. $AlreadyExist already exist. Mail wasn't filled in for $MailIsBlank."
ASKER
No, i have see the ) in ($($PDON)) and i have already added it!
the problem is when i have to add the contact without sn, givenname or witjhout office... and the script not work in this case
the problem is when i have to add the contact without sn, givenname or witjhout office... and the script not work in this case
Sorry, had to go out, just got back.
-Delimiter, introduced with PowerShell 2, so I guess you're still running PS 1.
So without those attributes you have nothing to form names on. If those fields are blank what would you like to do with the names / aliases?
Chris
-Delimiter, introduced with PowerShell 2, so I guess you're still running PS 1.
So without those attributes you have nothing to form names on. If those fields are blank what would you like to do with the names / aliases?
Chris
I will defer back to Chris.
The problem is probably with the Alias since $null is not a valid part of an alias. But my head is really foggy right now, so I will defer. If this isn't answered for you when I can think clearer, I'll see if I can address it again.
Yep, absolutely, so as long as we're building these like this:
$Name = "$($_.sn) $($_.givenName) ($($_.physicalDeliveryOffi ceName))"
$Alias = "$($_.givenName).$($_.sn)" .ToLower()
We will be stuck if any / all of those fields are blank. We can, of course, test to see if they are and act on that, but we'd have to know what we're to do :)
Chris
$Name = "$($_.sn) $($_.givenName) ($($_.physicalDeliveryOffi
$Alias = "$($_.givenName).$($_.sn)"
We will be stuck if any / all of those fields are blank. We can, of course, test to see if they are and act on that, but we'd have to know what we're to do :)
Chris
ASKER
Chris,
Simple i have 2 case:
1-If the givenname is empty, just make display name with the sn (office) and alias with only the sn
2-if the mail value is blank, ignore the contact and not create it
Exemple:
sn,givenName,physicalDeliv eryOfficeN ame,mail
SMITH,,Atlanta,john.smith@ domaineA.c om ==>here, the display name must SMITH (Atlanta) and alias must be smith, or if it possible alias=mail entry without @domaineA.com and here we can use the full alias john.smith
for the second exemple:
sn,givenName,physicalDeliv eryOfficeN ame,mail
SMITH,John,Atlanta,==> not email adress, the script must ignore this entry and do not create the contact
Very thanks
Simple i have 2 case:
1-If the givenname is empty, just make display name with the sn (office) and alias with only the sn
2-if the mail value is blank, ignore the contact and not create it
Exemple:
sn,givenName,physicalDeliv
SMITH,,Atlanta,john.smith@
for the second exemple:
sn,givenName,physicalDeliv
SMITH,John,Atlanta,==> not email adress, the script must ignore this entry and do not create the contact
Very thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hi chris,
physicalDeliveryOfficeName never empty
sn never empty
just givenname in certain case can be empty.
and i am in powershell 1 not 2 for -Delimiter ";" :)
physicalDeliveryOfficeName
sn never empty
just givenname in certain case can be empty.
and i am in powershell 1 not 2 for -Delimiter ";" :)
Sorry, I forgot to take the Delimiter bit out :) Other than that, it should be good to go :)
Chris
Chris
ASKER
Chris, you are genious its work and when the givenname is empty, your script use the mail adresse to build the alias :)
and when mailadress is empty, your script ignore it and not create the contact :)
thanks and if you can help me in this question:
https://www.experts-exchange.com/questions/26985192/powershell-script-to-export-mailbox-user-to-a-csv-file.html
and when mailadress is empty, your script ignore it and not create the contact :)
thanks and if you can help me in this question:
https://www.experts-exchange.com/questions/26985192/powershell-script-to-export-mailbox-user-to-a-csv-file.html
ASKER
Plz help me :)