Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Pwershell script to import external contact in Exchange 2007-AD

Posted on 2011-04-28
29
Medium Priority
?
804 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:cawasaki
  • 16
  • 7
  • 6
29 Comments
 

Author Comment

by:cawasaki
ID: 35489677
Hi,

Plz help me :)
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 35489876
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
0
 

Author Comment

by:cawasaki
ID: 35489899
Hi Chris,

I test :)
0
Who's Defending Your Organization from Threats?

Protecting against advanced threats requires an IT dream team – a well-oiled machine of people and solutions working together to defend your organization. Download our resource kit today to learn more about the tools you need to build you IT Dream Team!

 

Author Comment

by:cawasaki
ID: 35489931
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

0
 

Author Comment

by:cawasaki
ID: 35489936
and it say 2 already exist but not exist!
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 35489960
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
0
 

Author Comment

by:cawasaki
ID: 35489988
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
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 35490006
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
0
 

Author Comment

by:cawasaki
ID: 35490021
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.
0
 

Author Comment

by:cawasaki
ID: 35490040
ok, i have delete the "-Delimiter ";"" from the script and work better.

0
 

Author Comment

by:cawasaki
ID: 35490047
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) {

0
 

Author Comment

by:cawasaki
ID: 35490144
Chris?
0
 
LVL 12

Expert Comment

by:GusGallows
ID: 35490489
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"

0
 

Author Comment

by:cawasaki
ID: 35490526
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
0
 
LVL 12

Expert Comment

by:GusGallows
ID: 35491027
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

0
 

Author Comment

by:cawasaki
ID: 35491165
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
0
 
LVL 12

Expert Comment

by:GusGallows
ID: 35491897
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

0
 

Author Comment

by:cawasaki
ID: 35492056
sorry, not work
0
 
LVL 12

Expert Comment

by:GusGallows
ID: 35492125
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

0
 

Author Comment

by:cawasaki
ID: 35492162
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
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 35492290
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
0
 
LVL 12

Expert Comment

by:GusGallows
ID: 35492343
I will defer back to Chris.
0
 
LVL 12

Expert Comment

by:GusGallows
ID: 35492390
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.
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 35492410
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
0
 

Author Comment

by:cawasaki
ID: 35492449
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
0
 
LVL 71

Accepted Solution

by:
Chris Dent earned 2000 total points
ID: 35492552
Will sn and physicalDeliveryOfficeName ever be empty? This assumes not.
$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 {

      If ($_.sn -And $_.givenName -And $_.physicalDeliveryOfficeName) {

        $Name = "$($_.sn) $($_.givenName) ($($_.physicalDeliveryOfficeName))"
        $Alias = "$($_.givenName).$($_.sn)".ToLower()

      } Else {

        $Name = "$($_.sn) ($($_.physicalDeliveryOfficeName))"
        $Alias = ($_.mail -Replace '@.*$').ToLower()

      }

      $Contact = New-MailContact -Name $Name -ExternalEmailAddress $_.mail -DisplayName $Name `
        -Alias $Alias -FirstName $_.givenName -LastName $_.sn -OrganizationalUnit $OU
      If ($_.physicalDeliveryOfficeName) {
        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

Chris
0
 

Author Comment

by:cawasaki
ID: 35492566
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 ";"  :)
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 35492598
Sorry, I forgot to take the Delimiter bit out :) Other than that, it should be good to go :)

Chris
0
 

Author Comment

by:cawasaki
ID: 35492632
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:

http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_26985192.html
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

569 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