Link to home
Start Free TrialLog in
Avatar of cawasaki
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,physicalDeliveryOfficeName,mail
SMITH,John,Atlanta,john.smith@domaineA.com
HIRTEN,Christina,France,christina.hirten@domaineb.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 (physicalDeliveryOfficeName)

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.
Avatar of cawasaki
cawasaki

ASKER

Hi,

Plz help me :)
Avatar of Chris Dent
Hi there,

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."

Open in new window

Cheers,

Chris
Hi Chris,

I test :)
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.

Open in new window

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.
$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."

Open in new window

Chris
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
Righto :)
$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."

Open in new window

Cheers,

Chris
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.
ok, i have delete the "-Delimiter ";"" from the script and work better.

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) {

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"

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,physicalDeliveryOfficeName,mail
SMITH,John,john.smith@domaineA.com
HIRTEN,,France,christina.hirten@domaineb.com

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."

Open in new window

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
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."

Open in new window

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."

Open in new window

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
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
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) ($($_.physicalDeliveryOfficeName))"
     $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
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,physicalDeliveryOfficeName,mail
SMITH,,Atlanta,john.smith@domaineA.com  ==>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,physicalDeliveryOfficeName,mail
SMITH,John,Atlanta,==> not email adress, the script must ignore this entry and do not create the contact

Very thanks
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 ";"  :)
Sorry, I forgot to take the Delimiter bit out :) Other than that, it should be good to go :)

Chris
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