To create in bulk Mail Contact using Powershell. My program asks me to input manually the ExternalEmailAddres. When I do so, the program works. I can not do that because of the big number of Mail contact to create.
I just do not know see where my error is. Anyone can help? Please.
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://xxx.yyy.com/PowerShell/ -Authentication Kerberos -Credential $UserCredential
Import-PSSession $Session -DisableNameChecking
Import-Module ActiveDirectory
$User = Import-CSV C:\CREATE-MAIL-CONTACT.csv
$Params = @{
Name = $user.Name
ExternalEmailAddress = $User.PrimarySmtpAddress
OrganizationalUnit = "OU=Mail Contacts,DC=xxx,DC=com"
}
New-MailContact @Params
My CSV:
------
Displayname Name PrimarySmtpAddress
John Smith John Smith john.smith@abc.com
What your code is missing is to iterate over each row of your CSV, one way to do that is with ForEach-Object
:
Import-CSV C:\CREATE-MAIL-CONTACT.csv | ForEach-Object {
try {
$Params = @{
Name = $_.Name
ExternalEmailAddress = $_.PrimarySmtpAddress
OrganizationalUnit = "OU=Mail Contacts,DC=xxx,DC=com"
}
New-MailContact @Params
}
catch {
# error handling here
Write-Warning $_.Exception.Message
}
}