Top Menu

Showing posts with label Active Directory. Show all posts
Showing posts with label Active Directory. Show all posts

Wednesday, October 15, 2014

Export AD Users from a Group via PowerShell

I have to write this script recenely to export user and their few properties via PowerShell.
Import-Module ActiveDirectory

$ADGroup = "GroupName"
write-host "Accessing all users from group:" $ADGroup
Get-ADGroupMember -identity $ADGroup -recursive | select distinguishedName,name | Export-csv -path C:\Temp\users.csv -NoTypeInformation
write-host "Done."

Watch For:
You might see the following error:
Get-ADGroupMember : The size limit for this request was exceeded

Reason:
Default number of objects returned from AD are limited to 5,000 by default.

Resolution: Open the config file for ADWS
C:\Windows\ADWS\Microsoft.ActiveDirectory.WebServices.exe.config
and add the following line under <appsettings> tag
<add key="MaxGroupOrMemberEntries" value="25000">

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."

Friday, March 7, 2014

Export AD Users

I had a requirement to compare User Profile data from SharePoint with AD data and I found this little tool to export all the AD users into CSV file. Nice and easy.

CSVDE -f c:\Temp\AD_Users.csv -r objectClass=user

Other example would be

CSVDE -f AD_Users.csv -r "(&(objectClass=user)(objectCategory=person)"

and

CSVDE -d "OU=Directors,DC=domain,dc=local" -f test.csv -r "(&(objectClass=user)(objectCategory=person)"

Ref:  http://www.techrepublic.com/blog/data-center/simplify-admin-tasks-by-exporting-active-directory-data-with-csvde/#.

Official SharePoint Documentation

I have recently contributed to the official SharePoint documentation for developement. Check it out here: https://docs.microsoft.com/en-us...