Author Posts

October 16, 2017 at 8:32 pm

I have an exchange user import script that works quite well to import/edit/soon delete (i hope), up until now. The reason being is that we have 1 entity who has 1200+ users that i need to import into the respected mail contacts in AD. What i'm finding is said entity isn't playing nice and is leaving many fields blank and incorrect so it messes things up. When the fields used are exact and correct this is perfect, but sometimes they leave first name blank and have first and last in the last name field. Sometimes both first and last are blank and in displayed it's first and last. That being said, i want to import it exactly as is. How can I over come the blank fields?

#Adds, updates, and removes contacts


Import-Module ActiveDirectory


$BulkUserFile = Get-ChildItem C:\Users\jscharfenberg\Documents\Entities\AspenSnowMass\Mailboxes.csv
$FileImport = import-csv $BulkUserFile

$ContactOU = "OU=CompanyA,OU=Contacts,DC=DC=Mycompany,DC=com"

#Updates information for existing Contacts and adds new contacts
Foreach ($User in $FileImport)
{
$DispN = $User.'Display Name'.Trim()
$FN = $User.'First Name'.Trim()
$LN = $User.'Last Name'.Trim()
$Email = $User.'Email Address'.Trim()
$Title = $User.'Title'.Trim()
$Phone = $User.'Phone Number'.Trim()
$Street = $User.'Street'.Trim()
$City = $User.'City'.Trim()
$State = $User.'State'.Trim()
$Zip = $User.'Zip'.Trim()
#$Alias = $User.'Alias'.Trim()
#$NewAlias = $Alias + "CCI"



$CUser = Get-MailContact $DispN -ErrorAction SilentlyContinue| Select DisplayName
$DN = $CUser.DisplayName

If ($DN -ne $null)
{
Set-Contact -Identity $DispN -FirstName $FN -LastName $LN -Title $Title -Phone $Phone -StreetAddress $Street -City $City -StateOrProvince $State -PostalCode $Zip -Confirm:$False
}
Else
{
New-MailContact -Name $DispN -ExternalEmailAddress $Email -OrganizationalUnit $ContactOU -Confirm:$False
Sleep 10
Set-Contact -Identity $DispN -FirstName $FN -LastName $LN -ExternalEmailAddress $Email -Title $Title -Phone $Phone -StreetAddress $Street -City $City -StateOrProvince $State -PostalCode $Zip -Confirm:$False
}
}

#Checks to see if there are old users in the Contact OU to delete


After i get that i plan on working on the bottom part to remove old/deleted users so that the imports always match the CSV sent to me.

Any suggestions?

thanks all!!!

October 16, 2017 at 8:39 pm

Given how you've approached this (and there's nothing wrong with your approach), it'd be tough. What I'd do instead is build a hash table of parameters, rather than pulling everything into variables. That way, for each one, you can do a check to see if it's blank.

$params = @{}
ForEach ($User in $FileImport) {
 If ($User.'Display Name') { $params.add('Identity',($User.'Display Name').Trim()) }
}

Kinda like that for each field.

New-MailContact @params

Will then "splat" that hash table into the parameters. That way, you can dynamically omit parameters that aren't provided, along with doing any other data validation or manipulation you feel like doing. In that Add() method, the first argument is the parameter name you're providing, and the second is the value you want to pass to said parameter.