Thursday, March 20, 2014

Compare AD Properties to SharePoint UPA Properties

Scenario: I had a situation where I had to compare the properties of users from Active Directory to User Profile.

Solution:
  1. Export all users from Active Directory to CSV. How?
  2. Export all users from User Profile Application to CSV. How?
  3. Compare columns between two CSV files using the PowerShell script (ADvsUPAValidation.ps1) below.
#
# Author: Tahir Naveed
# Created: Mar 13, 2014
# Modified: Mar 13, 2014
# Description:     
# This script compares AD properties with UPA properties for a user
#
#

function WriteLog
{
    Param([string]$message, [string]$logFilePath)
    Add-Content -Path $logFilePath -Value $message
}

$LogFile = "G:\PowerShellScripts\ADvsUPAValidation\ADvsUPA_Result.log"
$ADFile = "G:\PowerShellScripts\ADvsUPAValidation\ADexport.csv"  
$UPAFile = "G:\PowerShellScripts\ADvsUPAValidation\UPAexport.csv"

$ADProfileCount = 0
$ADUsers = Import-CSV $ADFile | sort sAMAccountName
$TotalADProfiles = $ADUsers.Count


ForEach ($ADUser in $ADUsers) 
{
    $ADProfileCount ++;

    try
    {  
        # Search AD User in UPA
        $UPAUser = Import-CSV $UPAFile | where-object {$_.UserName -eq $ADUser.sAMAccountName}
        
        $Now = [System.DateTime]::Now
        $MSG = $Now.ToString() +  " | Working on "+ $ADProfileCount + " of " + $TotalADProfiles + " - " +$ADUser.sAMAccountName 
        write-host $MSG

        if(($UPAUser.FirstName -ne $null)-and($ADUser.givenName -ne $null)-and($UPAUser.FirstName -ne $ADUser.givenName))
        {
            $MSG = "FirstName mismatch:"+ $UPAUser.UserName + ":UPA:" + $UPAUser.FirstName+ ":AD:" + $ADUser.givenName
            write-host -f red  $MSG
            WriteLog $MSG $LogFile
        }
        if(($UPAUser.LastName -ne $null)-and($ADUser.sn -ne $null)-and($UPAUser.LastName -ne $ADUser.sn))
        {
            $MSG = "LastName mismatch:"+ $UPAUser.UserName + ":UPA:" + $UPAUser.LastName+ ":AD:" + $ADUser.sn
            write-host -f red  $MSG
            WriteLog $MSG $LogFile
        }
        if(($UPAUser.PreferredName -ne $null)-and($ADUser.displayName -ne $null)-and($UPAUser.PreferredName -ne $ADUser.displayName))
        {
            $MSG = "PreferredName mismatch:"+ $UPAUser.UserName + ":UPA:" + $UPAUser.LastName+ ":AD:" + $ADUser.displayName
            write-host -f red  $MSG
            WriteLog $MSG $LogFile
        }

    }
    catch [system.exception]
    {
        $Now = [System.DateTime]::Now
        $MSG = $Now.ToString() + " | "+ $ADUser +" | Exp | " + $_.Exception.Message
        write-host -f red $MSG
        WriteLog $MSG $LogFile
    }

    $User = $Null
}

write-host "Done."

Knowledge Chat Bot (No Code)

Recently my customers was looking for a solution where on-field guys can search for answers related to their tasks and it was a perfect scen...