Sunday, August 1, 2010

Home /Active Directory /PowerShell /SamAccountName /script /Windows / Search Users from Multiple Domains Using a Common Attribute–PowerShell Script

Search Users from Multiple Domains Using a Common Attribute–PowerShell Script

This script searches 2 Active Directory domains using a common matching attribute value (employeeID) and creates an out file with Source SamAccountName, Target SamAccountName and Name.  You can use this script if you are performing user migrations and merging user accounts during the migration.

image

Input file – Empid.csv – contains all employee IDs.

image

Output file – UserInfo.txt - The script generates a TAB delimited file output which contains Source SamAccountName, Target SamAccountName and Name files.

image

In this script I am using employeeID as the common attribute to search user account in the source (ss-infra.lab) and target (santhosh.lab) domains.

Download:

http://www.sivarajan.com/scripts/Search_Multiple_Domains_Using_EmpID.txt

SS

Santhosh Sivarajan

Microsoft MVP · Identity & Cybersecurity Architect

Santhosh has 30+ years of hands-on enterprise experience in Identity and Access Management, Microsoft Entra ID, Active Directory, Microsoft 365 and Zero Trust architecture. He is the author of two books on Windows Server and security, and leads consulting, assessments and training at SAG Business Group.

3 comments:

During migrations we often require to find duplicate accounts ( users, groups or computers) between source and target domains. How can this script be used to accomplish this. Thanks.

Duplicate Names? It depends on the attribute. You can change the attribute filter in the script. I am using Employee ID. Also update the input file with correct values.

Hi Santosh,

need your help.. I want a script which will read users NT name from txt file. search in three child trusted domains & if found add them to a group. This group is located in domain1.

Users are located in multiple OUs and could reside in any domain.
Input file users.txt contains only user name in the following format:

user12345
user23456
user34567
user45678
so on.....

Can you please help asap.
======================
Option Explicit

Dim objRootDSE, strDNSDomain, objTrans, strNetBIOSDomain
Dim strFile, objFSO, objFile, strNTName, strDN, objGroup, strGroup
Dim objUser

Const ForReading = 1
' Constants for the NameTranslate object.
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1

' Specify Network path of file of user names.
strFile = "\\server\c$\test\users.csv"
strGroup = "DL_TEST_SCRIPT_GROUP"

strNetBIOSDomain1 = "domain1_local"
strNetBIOSDomain2 = "domain2.local"
strNetBIOSDomain3 = "domain3.local"

' Bind to the group object in Active Directory, using the WinNT provider.
On Error Resume Next
Set objGroup = GetObject("WinNT://" & strNetBIOSDomain1 & "/" _
& strGroup & ",group")

' Use FSO to open text file for read access.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFile, ForReading)


' Read the file.
Do Until objFile.AtEndOfStream
' Read user name.
strNTName = Trim(objFile.ReadLine)
' Skip blank lines.
If (strNTName <> "") Then
On Error Resume Next
Set objUser = GetObject("WinNT://" & strNetBIOSDomain1 & "/" _
& strNTName & ",user")
If (Err.Number <> 0) Then
On Error GoTo 0
End If

Set objUser = GetObject("WinNT://" & strNetBIOSDomain2 & "/" _
& strNTName & ",user")
If (Err.Number <> 0) Then
On Error GoTo 0
End If

Set objUser = GetObject("WinNT://" & strNetBIOSDomain3 & "/" _
& strNTName & ",user")
If (Err.Number <> 0) Then
On Error GoTo 0
End If
Else

' Check if the user is already a member of the group.
If (objGroup.IsMember(objUser.AdsPath) = True) Then
Wscript.Echo "User " & strNTName & " is already member of specified group."
Else
' Add user to the group.
objGroup.Add(objUser.AdsPath)
Wscript.Echo "User " & strNTName & " has been added to Group."
End If
End If
Loop

' Clean up.
objFile.Close


Thanks, MPG

Post a Comment

Popular Posts
Workstation Trust Relationship Issue Issue: You receive the following error message, when you try to login to the domain.  The security database on the server does not have ... ADMT Service Account - Permission and Configuration The ADMT service account needs to have proper permission in source and target domains.  You don’t need to use 2 separate accounts.  You can ... My First Peek into Microsoft Exchange 2010 By Santhosh Sivarajan Before I really dive into Exchange 2010, I thought I would install and play with it first. I took some screen shots and notes during the ins... ObjectSID and Active Directory What is an objectSID in Active Directory? When a new object is created in Active Directory, Domain Controller assigns a unique value used ... AD Group Report - List Group Members in Active Directory–PowerShell Script Updated Script - http://portal.sivarajan.com/2011/10/search-ad-collect-local-admin-group.html Script #1 This script... Add Users to a Group–PowerShell Script Purpose – Add users to a group from an input file – PowerShell V2 Script.  Input file – Input file (Users.csv) contains samAccountName in... User Account Migration and Merging – Part I (ADMT) Part I - User Account Migration and Merging Using ADMT Part II - User Account Migration and Merging Using QMM pre-creating user account ... User Account Migration and Merging – Part II (Quest Migration Manager) Part I - User Account Migration and Merging Using ADMT Part II - User Account Migration and Merging Using QMM Pre-creating user account in... Delete Stale or Inactive Computer Accounts from Active Directory Here is an easy way to identify and delete inactive or stale computers in an Active Directory environment.  Using the dsquery command you c... Converting PowerShell (PS1) to EXE / Standalone Application As we know, there many applications available to convert a PowerShell file to a standalone executable file.  Based on my experience, PowerSh...