# Set the file location $rawfile = "X:\SCRIPTS\contacts.csv" # Ignore Error Messages and continue on. Comment this out with a # symbol if you need verbose errors trap [Microsoft.Exchange.Configuration.Tasks.ManagementObjectNotFoundException] { continue; } $contactfile = Import-CSV $rawfile # Read contacts and make the magic happen ForEach ($contact in $contactfile) { # Read all attributes provided by the file $sourceEMail=$contact.EMail $sourceInformalName=$contact.InformalName -replace " ","-" $sourceFirstName=$contact.FirstName -replace " ","-" $sourceLastName=$contact.LastName -replace " ","-" $sourceManagerID=$contact.ManagerID $sourceBranchCode=$contact.BranchCode $sourceBranchLegalName=$contact.BranchLegalName $sourceAddressLine1=$contact.AddressLine1 $sourceAddressLine2=$contact.AddressLine2 $sourceAddressLine3=$contact.AddressLine3 $sourceAddressCity=$contact.AddressCity $sourceAddressState=$contact.AddressState $sourceAddressZip=$contact.AddressZip $sourceAddressCountry=$contact.AddressCountry $sourceJobTitle=$contact.JobTitle $sourcePhoneCountryCode=$contact.PhoneCountryCode $sourcePhoneAreaCode=$contact.PhoneAreaCode $sourcePhoneNumber=$contact.PhoneNumber $sourcePhoneExtensionOrSpeedDial=$contact.PhoneExtensionOrSpeedDial # Create the concatenated fields and custom fields # Informal Name - This checks to see if they have an informal name (Jim versus James) and if so, use the informal name if ($sourceInformalName.Length -lt 1) { $targetFirstName = $sourceFirstName } elseif ($sourceInformalName.Length -gt 1) { $targetFirstName = $sourceInformalName } # Assign the Display Name using the correct First Name, Last Name and a suffix of COMPANY. We trim this field because of leading spaces that show up regularly $sourceDisplayName = "$targetFirstName $sourceLastName COMPANY" $targetDisplayName = $sourceDisplayName.Trim() # Assign the Distinguished Name attribute using the correct First Name, Last Name and OU structure $targetDistinguishedName = "CN=$targetFirstName $sourceLastName,OU=ExternalContacts,DC=yourdomain,DC=com" # Assemble the phone number # Check for a country code, otherwise value is null if ($sourcePhoneCountryCode -lt 1) { $targetCountryCode = $null } elseif ($sourcePhoneCountryCode -gt 1) { $targetCountryCode = "$sourceCountryCode-" } # Check for an extension, otherwise value is null if ($sourcePhoneExtensionOrSpeedDial -lt 1) { $targetExtension = $null } elseif ($sourcePhoneExtensionOrSpeedDial -gt 1) { $targetExtension = " ext. $sourcePhoneExtensionOrSpeedDial" } $targetPhoneNumber = "$targetCountryCode$sourcePhoneAreaCode-$sourcePhoneNumber$targetExtension" # Assemble the Address $targetStreetAddress = "$sourceAddressLine1 $sourceAddressLine2 $sourceAddressLine3" # Assign the name attribute for new contacts $targetCommonName = "$sourceFirstName $sourceLastName" # Assign the Alias using COMPANY as a prefix so that we can identify them easily $targetAlias = "COMPANY$targetFirstName$sourceLastName" ################################################### # Search for the contact to see if it is existing # ################################################### if ( Get-Contact -Identity $sourceEmail ) { # Output to screen so we can track the process. Comment the following line when it is running as a batch process Write-Host $sourceEmail Exists so $targetDisplayName will be MODIFIED -foregroundcolor green Set-MailContact -Identity $sourceEmail -Alias $targetAlias -ForceUpgrade Set-Contact -Identity $sourceEmail ` -City $sourceAddressCity ` -Company $sourceBranchLegalName ` -CountryOrRegion $sourceAddressCountry ` -Department $sourceBranchCode ` -DisplayName $targetDisplayName ` -SimpleDisplayName $targetDisplayName ` -Name "$targetCommonName" ` -FirstName $targetFirstName ` -LastName $sourceLastName ` -Phone $targetPhoneNumber ` -PostalCode $sourceAddressZip ` -StateOrProvince $sourceAddressState ` -StreetAddress $targetStreetAddress ` -Title $sourceJobTitle ` -WebPage "RJFAccountFlag" ` -WindowsEmailAddress $sourceEmail -WhatIf } ################################################### # If it is not existing, create a new contact # ################################################### else { # Output to screen so we can track the process. Comment the following line when it is running as a batch process Write-Host $sourceEmail Does Not Exist so $targetDisplayName will be CREATED -foregroundcolor yellow # First we create the contact with the required properties New-MailContact -Name "$targetCommonName" ` -OrganizationalUnit "OU=ExternalRJFContacts,DC=market,DC=financial,DC=local" ` -ExternalEmailAddress $sourceEmail ` -Alias $targetAlias ` -DisplayName $targetDisplayName ` -FirstName $targetFirstName ` -LastName $sourceLastName ` -PrimarySmtpAddress $sourceEmail -WhatIf # Now we set the additional properties that aren't accessible by the New-MailContact cmdlet Set-Contact -Identity $sourceEmail ` -City $sourceAddressCity ` -Company $sourceBranchLegalName ` -CountryOrRegion $sourceAddressCountry ` -Department $sourceBranchCode ` -DisplayName $targetDisplayName ` -SimpleDisplayName $targetDisplayName ` -FirstName $targetFirstName ` -LastName $sourceLastName ` -Phone $targetPhoneNumber ` -PostalCode $sourceAddressZip ` -StateOrProvince $sourceAddressState ` -StreetAddress $targetStreetAddress ` -Title $sourceJobTitle ` -WebPage "RJFAccountFlag" ` -WindowsEmailAddress $sourceEmail -WhatIf } # Clean up after your pet - this does some memory cleanup [System.GC]::Collect() [System.GC]::WaitForPendingFinalizers() } ################################################################## # Now we reverse the process and remove contacts no longer valid # # If contact is not in the file, delete it # ################################################################## $CurrentContacts = Get-MailContact -OrganizationalUnit 'OU=ExternalContacts,DC=yourdomain,DC=com' -ResultSize Unlimited | Select-Object PrimarySMTPAddress,name ForEach ($contact in $currentContacts) { $sourceEmail = $Contact.PrimarySMTPAddress if ( Get-Content $rawFile | Select-String -pattern $sourceEmail ) { # Output to screen so we can track the process. Comment the following line when it is running as a batch process Write-Host This contact $sourceEmail Exists in the file -foregroundcolor green } else { # Output to screen so we can track the process. Comment the following line when it is running as a batch process Write-Host "DELETE THIS CONTACT $sourceEmail" -backgroundcolor yellow Remove-MailContact -Identity "$sourceEmail" -Confirm:$Y -WhatIf } } $CurrentContacts = $null # Clean up after your pet - this does some memory cleanup [System.GC]::Collect() [System.GC]::WaitForPendingFinalizers()