Created
July 31, 2017 16:03
-
-
Save notesbytom/99d6af35b62454794e264d3329714df9 to your computer and use it in GitHub Desktop.
Synchronize CSV Contact List to Active Directory Global Address List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# NOTE: param() MUST BE FIRST STATEMENT in script file!!! | |
param([Parameter(Mandatory=$true)] $csvPath) | |
$ErrorActionPreference = "Stop" | |
Set-StrictMode -Version Latest | |
Import-Module ActiveDirectory | |
# Get the CSV file path from an input parameter ($csvPath) | |
$people = Import-CSV $csvPath | |
# CONVERT EMPTY STRING FIELDS TO $null | |
foreach ($person in $people) { | |
foreach ($property in $person.psobject.properties) { | |
if ($property.Value.Length -eq 0) { # zero length string | |
$property.Value = $null; # convert to $null | |
}}} | |
# NOW WE CAN COMPARE NULL VALUES ... | |
foreach ($person in $people) { | |
$adUser = $null # default value | |
# Must specify ALL RELEVANT PROPERTIES otherwise they will NOT BE RETRIEVED with user objects | |
try { $adUser = Get-AdUser $person.login -Properties otherTelephone,MobilePhone,OfficePhone,pager,Department,Manager } | |
catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] { } # keep null | |
if ($adUser -ne $null) { | |
#"Found AD User $($person.login)" | |
# Lookup manager distinguished name in AD | |
$mgrDN = $null # default value | |
try { | |
if ($person.mgrLogin -ne $null) { | |
$mgrDN = (Get-AdUser $person.mgrLogin).DistinguishedName | |
} | |
} catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] { } # keep null | |
$change = $false | |
if ( | |
($person.offsitePhone -eq $null -and $adUser.otherTelephone.Count -gt 0) -or # missing in ADP, present in AD | |
($person.offsitePhone -ne $null -and $adUser.otherTelephone.Count -gt 0 -and $person.offsitePhone -ne $adUser.otherTelephone[0]) -or # present in both, different values | |
$person.cellPhone -ne $adUser.MobilePhone -or | |
$person.officePhone -ne $adUser.OfficePhone -or | |
$person.pager -ne $adUser.pager -or | |
$person.dept -ne $adUser.Department -or | |
$mgrDN -ne $adUser.Manager | |
) { | |
# records are different, update AD-Object with data from ADP | |
"=" * 48 | |
"Record before change \\\\\\\/////" | |
$adUser | Select Name,SamAccountName,otherTelephone,MobilePhone,OfficePhone,pager,Department,Manager | |
if ($person.offsitePhone -ne $null) { # we have an offsite phone | |
if ($adUser.otherTelephone.Count -lt 1) { # need to expand list (currently empty) | |
$null = $adUser.otherTelephone.Add("") | |
} | |
$adUser.otherTelephone[0] = $person.offsitePhone | |
} else { # we don't have an offsite phone | |
$adUser.otherTelephone.Clear() # clear list in AD | |
} | |
$adUser.MobilePhone = $person.cellPhone | |
$adUser.OfficePhone = $person.officePhone | |
$adUser.pager = $person.pager | |
$adUser.Department = $person.dept | |
$adUser.Manager = $mgrDN | |
"Record with updates \\\\\\\/////" | |
$adUser | Select Name,SamAccountName,otherTelephone,MobilePhone,OfficePhone,pager,Department,Manager | |
Set-ADUser -Instance $adUser #write the changes | |
} | |
} else { | |
"MISSING AD USER $($person.login)" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example to synchronize contents of CSV file to Active Directory / Microsoft Exchange Global Address List (AD GAL). This is a typical load task from an ETL project (Extract, Transform, Load). PowerShell makes the task a little easier. Note that we're using pipeline output to show the object before and after modification - this can be saved to a log file for review if desired.