Batch file to extract user profile from OU

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
yokoyoko
Posts: 9
Joined: 26 Dec 2012 20:55

Batch file to extract user profile from OU

#1 Post by yokoyoko » 26 Dec 2012 21:00

Hi guys,

I am new here and new to batch scripting. I will need to extract all the user list (user name, id, type, description) from the OU in the DC by using batch file to automate this process as there are many different OUs in the DC which will be timing consuming to manually export one by one.

Any help is appreciated!

Thank you!

Regards,
Manu

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file to extract user profile from OU

#2 Post by foxidrive » 26 Dec 2012 21:30

Export a line and paste it here, outlining which parts you want.

I assume each line will be one record and it repeats the format on each line.

yokoyoko
Posts: 9
Joined: 26 Dec 2012 20:55

Re: Batch file to extract user profile from OU

#3 Post by yokoyoko » 27 Dec 2012 02:59

Hi foxidrive,

To export something like below from multiple OUs.

Name Type Description Pre-Windows 2000 Logon Name
Lucy Camp User HR LucyCa
Vince Porter User HR VincePo

Thank you.
Regards,
Manu

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file to extract user profile from OU

#4 Post by foxidrive » 27 Dec 2012 03:02

I need to know what multiple OU's look like when they are exported - so the batch file can parse them and extract the data.

yokoyoko
Posts: 9
Joined: 26 Dec 2012 20:55

Re: Batch file to extract user profile from OU

#5 Post by yokoyoko » 27 Dec 2012 03:49

Actually i have no idea how it will look like too.

Current way of doing is manually export one by one from the OU to different txt or csv files. The format is what i have pasted earlier on.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch file to extract user profile from OU

#6 Post by abc0502 » 27 Dec 2012 05:23

Can you describe more ?
unless there is a static pattern in that file, i mean that each piece of data you want to extract always in a specific location in the file, or there is something unique to identify it like a line number or a word, the batch file will be useless.

yokoyoko
Posts: 9
Joined: 26 Dec 2012 20:55

Re: Batch file to extract user profile from OU

#7 Post by yokoyoko » 27 Dec 2012 07:08

Basically, there are multiple Organization Units (OUs) in the Domain controller. OU with naming like marketing, HR, IT (basically the department name). These OUs contain the following details:

1. Name
2. Type
3. Description
4. Pre-Windows 2000 Logon Name

Current process is that i need to go into individual OU to export the user details (above) and to repeat multiple times.

I am thinking of executing batch file to export the user details from the OUs to a csv or txt file without manually export like what i am doing now. Is it possible or is there a better way of doing such manual work?

Thanks for helping. :D

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch file to extract user profile from OU

#8 Post by abc0502 » 27 Dec 2012 07:56

ok , then how these data :
1. Name
2. Type
3. Description
4. Pre-Windows 2000 Logon Name
exist in the files?

if it possible to post a samble for each file so we can see how will these data be extracted. as Foxidrive said
I need to know what multiple OU's look like when they are exported - so the batch file can parse them and extract the data.


as how we know that a specific word in the file is the name or the type or any other data ??

DigitalSnow
Posts: 20
Joined: 21 Dec 2012 13:36
Location: United States

Re: Batch file to extract user profile from OU

#9 Post by DigitalSnow » 27 Dec 2012 09:27

Have you looked at or are you using Ldifde?

Ldifde
Ldifde Examples
Using LDIFDE to export and to import directory objects

The following is a step-by-step process to import and export Organizational Units (OUs) and user accounts from one Windows 2000 Active Directory to another. For this example, "Export" is the name of the domain from which objects are exported and "Import" is the name of the domain in which objects are imported. LDIFDE can also be used to import most third-party folders to Active Directory.

Export the organizational units from the source domain

Log on as Administrator in the Export domain. If you log on using an account that does not have administrative privileges, you may not be able to perform export and import operations against the Active Directory.
Click Start, point to Programs, point to Accessories, and then click Command Prompt.
At the command prompt, type:

Code: Select all

ldifde -f exportOu.ldf -s Server1 -d "dc=Export,dc=com" -p subtree -r "(objectCategory=organizationalUnit)" -l "cn,objectclass,ou"

Running this command exports all OUs except domain controllers into a file named ExportOU.ldf.

Export the user accounts from the source domain

At the command prompt, type:

Code: Select all

ldifde -f Exportuser.ldf -s Server1 -d "dc=Export,dc=com" -p subtree -r "(&(objectCategory=person)(objectClass=User)(givenname=*))" -l "cn,givenName,objectclass,samAccountName"

Running this command exports all users in the Export domain into a file named Exportuser.ldf. If you do not have all the required attributes, the import operation does not work. The attributes objectclass and samAccountName are required, but more can be added as needed.

Note Built-in accounts, such as Administrator, do not have a given name. By default, the LDAP filter used above does not export those accounts. LDIFDE does not support exporting passwords.

Note When you omit the -s parameter, LDIFDE chooses a global catalog for exports. Depending on the domain controller placement, this server might be a domain controller for a different domain, and exports might fail. You see this when you examine the output of LDIFDE. In this case, specify a local domain controller of the domain in which the objects are hosted.


More Ldifde Examples: http://support.microsoft.com/kb/555636
Organizational Units (OU): http://technet.microsoft.com/en-us/library/cc758565(WS.10).aspx

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Batch file to extract user profile from OU

#10 Post by Ed Dyreen » 27 Dec 2012 09:42

yokoyoko wrote:Hi foxidrive,

To export something like below from multiple OUs.

Name Type Description Pre-Windows 2000 Logon Name
Lucy Camp User HR LucyCa
Vince Porter User HR VincePo

Thank you.
Regards,
Manu
Name|Type|Description|Pre-Windows 2000 Logon Name

Ok so it's space delimited, but that causes issues, how to determine wheter "Porter" is part of Name ?
If optional spaces only occur in Name, it is possible to search from the right to the left.
Thus counting all items beforehand minus 3 would be name.

But this fail if one of Type|Description|Pre-Windows 2000 Logon Name have spaces in them :!:

Code: Select all

@echo off &setlocal enableDelayedExpansion


set "myFile=myNamesIndex.TXT"

for /f "useback delims=" %%? in (

   "!myFile!"

) do (
   echo.
   echo.?=%%?_

   set "i=" &for %%? in ( %%? ) do set /a i += 1 &set "array[!i!]=%%~?"

   call set "Pre-Windows 2000 Logon Name=%%array[!i!]%%"
   set "Pre-Windows 2000 Logon Name"
   set /a i -= 1

   call set "Description=%%array[!i!]%%"
   set "Description"
   set /a i -= 1

   call set "Type=%%array[!i!]%%"
   set "Type"
   set /a i -= 1

   set "Name=" &for /l %%? in ( 1, 1, !i! ) do set "Name=!Name! !array[%%?]!"
   set "Name=!Name:~1!"
   set "Name"
)

pause
exit

Code: Select all


?=Eddy User HR VincePo_
Pre-Windows 2000 Logon Name=VincePo
Description=HR
Type=User
Name=Eddy

?=Vince Porter User HR VincePo_
Pre-Windows 2000 Logon Name=VincePo
Description=HR
Type=User
Name=Vince Porter

?=Herman Von Reuter User HR VincePo_
Pre-Windows 2000 Logon Name=VincePo
Description=HR
Type=User
Name=Herman Von Reuter
Druk op een toets om door te gaan. . .
myNamesIndex.TXT

Code: Select all

Eddy User HR VincePo
Vince Porter User HR VincePo
Herman Von Reuter User HR VincePo
It would be easier if you could export to CSV format.

ed

billrich
Posts: 70
Joined: 24 Apr 2012 05:36
Location: USA

Re: Batch file to extract user profile from OU

#11 Post by billrich » 27 Dec 2012 17:44

What is OU?

DigitalSnow
Posts: 20
Joined: 21 Dec 2012 13:36
Location: United States

Re: Batch file to extract user profile from OU

#12 Post by DigitalSnow » 27 Dec 2012 18:23

billrich wrote:What is OU?


See the bottom of my post above. They are Organizational Units for Active Directory in Windows Server.


yokoyoko
Posts: 9
Joined: 26 Dec 2012 20:55

Re: Batch file to extract user profile from OU

#13 Post by yokoyoko » 27 Dec 2012 21:57

abc0502 wrote:ok , then how these data :
1. Name
2. Type
3. Description
4. Pre-Windows 2000 Logon Name
exist in the files?

if it possible to post a samble for each file so we can see how will these data be extracted. as Foxidrive said
I need to know what multiple OU's look like when they are exported - so the batch file can parse them and extract the data.


as how we know that a specific word in the file is the name or the type or any other data ??


Hi,

The user information in the OUs are exported to individual txt. file which will look like the follow with spacing between (not sure why when i pasted here, the spacing is gone):

Name Type Description Pre-Windows 2000 Logon Name
Amy Joe User MAMS amyjoe

Example of OUs :

http://www.windowsitpro.com/content/content/27330/Figure_01.jpg

yokoyoko
Posts: 9
Joined: 26 Dec 2012 20:55

Re: Batch file to extract user profile from OU

#14 Post by yokoyoko » 27 Dec 2012 21:59

DigitalSnow wrote:Have you looked at or are you using Ldifde?

More Ldifde Examples: http://support.microsoft.com/kb/555636
Organizational Units (OU): http://technet.microsoft.com/en-us/libr ... 8565(WS.10).aspx


Hi,

I tried this but CMD shows no entries found. Not sure why it didnt work.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch file to extract user profile from OU

#15 Post by abc0502 » 29 Dec 2012 21:03

Have you tried Ed Dyreen 's Code ?

Post Reply