Page 1 of 2

Batch file to extract user profile from OU

Posted: 26 Dec 2012 21:00
by yokoyoko
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

Re: Batch file to extract user profile from OU

Posted: 26 Dec 2012 21:30
by foxidrive
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.

Re: Batch file to extract user profile from OU

Posted: 27 Dec 2012 02:59
by yokoyoko
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

Re: Batch file to extract user profile from OU

Posted: 27 Dec 2012 03:02
by foxidrive
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.

Re: Batch file to extract user profile from OU

Posted: 27 Dec 2012 03:49
by yokoyoko
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.

Re: Batch file to extract user profile from OU

Posted: 27 Dec 2012 05:23
by abc0502
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.

Re: Batch file to extract user profile from OU

Posted: 27 Dec 2012 07:08
by yokoyoko
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

Re: Batch file to extract user profile from OU

Posted: 27 Dec 2012 07:56
by abc0502
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 ??

Re: Batch file to extract user profile from OU

Posted: 27 Dec 2012 09:27
by DigitalSnow
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

Re: Batch file to extract user profile from OU

Posted: 27 Dec 2012 09:42
by Ed Dyreen
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

Re: Batch file to extract user profile from OU

Posted: 27 Dec 2012 17:44
by billrich
What is OU?

Re: Batch file to extract user profile from OU

Posted: 27 Dec 2012 18:23
by DigitalSnow
billrich wrote:What is OU?


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


Re: Batch file to extract user profile from OU

Posted: 27 Dec 2012 21:57
by yokoyoko
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

Re: Batch file to extract user profile from OU

Posted: 27 Dec 2012 21:59
by yokoyoko
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.

Re: Batch file to extract user profile from OU

Posted: 29 Dec 2012 21:03
by abc0502
Have you tried Ed Dyreen 's Code ?