Import-CSV - Check Mail-Contact fields to see if changed and if so Set-Contact

%3CLINGO-SUB%20id%3D%22lingo-sub-2752702%22%20slang%3D%22en-US%22%3EImport-CSV%20-%20Check%20Mail-Contact%20fields%20to%20see%20if%20changed%20and%20if%20so%20Set-Contact%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2752702%22%20slang%3D%22en-US%22%3EI%20would%20like%20to%20Use%20my%20existing%20script%20to%20check%20if%20a%20Contact's%20fields%20below%20are%20different%20than%20the%20CSV%20and%20if%20they%20are%20use%20the%20CSV%20input%20to%20update%20each%20property%20on%20the%20Contact.%3CBR%20%2F%3E%3CBR%20%2F%3EHow%20would%20I%20do%20this%3F%3CBR%20%2F%3E%3CBR%20%2F%3EIf%20(Test-Path%20%24CSVFileName)%20%7B%3CBR%20%2F%3E%3CBR%20%2F%3E%23Import%20the%20CSV%20file%3CBR%20%2F%3E%24csvfile%20%3D%20Import-CSV%20%24CSVFileName%3CBR%20%2F%3E%3CBR%20%2F%3E%23Loop%20through%20CSV%20file%3CBR%20%2F%3Eforeach%20(%24line%20in%20%24csvfile)%20%7B%3CBR%20%2F%3E%3CBR%20%2F%3Etry%20%7B%3CBR%20%2F%3E%23Create%20the%20mail%20contact%3CBR%20%2F%3ENew-MailContact%20-domaincontroller%20%24dc%20-Name%20%24line.Name%20-ExternalEmailAddress%20%24line.ExternalEmailAddress%20-OrganizationalUnit%20%24OU%20-ErrorAction%20STOP%3CBR%20%2F%3ESet-Contact%20-domaincontroller%20%24dc%20-Identity%20%24line.Name%20-Department%20%24line.Department%20-Title%20%22Sales%20Team%22%20-StreetAddress%20%24line.StreetAddress%20-City%20%24line.City%20-StateOrProvince%20%24line.StateOrProvince%20-PostalCode%20%24line.PostalCode%20-Office%20%24line.Office%20-CountryOrRegion%20%24line.CountryOrRegion%3CBR%20%2F%3E%22%24(%24line.Name)%20was%20created%20successfully.%22%20%7C%20Out-File%20%24logfile%20-Append%3CBR%20%2F%3E%7D%3CBR%20%2F%3Ecatch%20%7B%3CBR%20%2F%3E%3CBR%20%2F%3E%24message%20%3D%20%22A%20problem%20occured%20trying%20to%20create%20the%20%24(%24line.Name)%20contact%22%3CBR%20%2F%3E%24message%20%7C%20Out-File%20%24logfile%20-Append%3CBR%20%2F%3EWrite-Warning%20%24message%3CBR%20%2F%3EWrite-Warning%20%24_.Exception.Message%3CBR%20%2F%3E%24_.Exception.Message%20%7C%20Out-File%20%24logfile%20-Append%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2752702%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExchange%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Regular Visitor
I would like to Use my existing script to check if a Contact's fields below are different than the CSV and if they are use the CSV input to update each property on the Contact.

How would I do this?

If (Test-Path $CSVFileName) {

#Import the CSV file
$csvfile = Import-CSV $CSVFileName

#Loop through CSV file
foreach ($line in $csvfile) {

try {
#Create the mail contact
New-MailContact -domaincontroller $dc -Name $line.Name -ExternalEmailAddress $line.ExternalEmailAddress -OrganizationalUnit $OU -ErrorAction STOP
Set-Contact -domaincontroller $dc -Identity $line.Name -Department $line.Department -Title "Sales Team" -StreetAddress $line.StreetAddress -City $line.City -StateOrProvince $line.StateOrProvince -PostalCode $line.PostalCode -Office $line.Office -CountryOrRegion $line.CountryOrRegion
"$($line.Name) was created successfully." | Out-File $logfile -Append
}
catch {

$message = "A problem occured trying to create the $($line.Name) contact"
$message | Out-File $logfile -Append
Write-Warning $message
Write-Warning $_.Exception.Message
$_.Exception.Message | Out-File $logfile -Append
}

}
}

1 Reply

@TechGuy1815  Something like this:

if (!Test-Path $CSVFileName) 
{
    Write-Error "Cannot find $CSVFileName"
    exit(1)
}

foreach ($line in $csvfile) 
{
    $contact = Get-Contact -domaincontroller $dc -Identity $line.Name -ErrorAction SilentlyContinue
    if($contact)
    {
        # The Contact exists, check details
        if( 
            ($contact.Department -ne $line.Department) -or 
            ($contact.Title -ne $line.Title) -or 
        )
        {
            # update ALL of the contacts details if any detail is different
            Set-Contact ...
        }
    }
    else 
    {
        # new contact
        New-MailContact ...
        Set-Contact ...
    }
}

 

but if the script is not run very often, or if there are not too many users listed int he file, you could try:

if (!Test-Path $CSVFileName) 
{
    Write-Error "Cannot find $CSVFileName"
    exit(1)
}

foreach ($line in $csvfile) 
{
    $contact = Get-Contact -domaincontroller $dc -Identity $line.Name -ErrorAction SilentlyContinue
    if(!$contact)
    {
        # Contact does not exist, so create a new contact
        New-MailContact ...
    }

    # Always update the contact details cause the caontact exists now
    Set-Contact ...
}

In this second option you always update the contact details from the data in the file.

 

Definitely try this in a test environment first.