4 Replies - 557 Views - Last Post: 15 April 2016 - 12:06 PM Rate Topic: -----

#1 RamonRobben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 82
  • View blog
  • Posts: 544
  • Joined: 19-May 14

BATCH add OU's in server 2012 r2 using .csv file

Posted 15 April 2016 - 02:34 AM

So I'm trying to add ou's to my windows server 2012 r2.
I try to do this by looping trough a .csv file that will add them in a nice structure.
The problem is its not doing anything other then printing out the code.
Also some variables are empty but I have If statements trying to figure that out.
Here is my code:

for /F "tokens=1,2,3,4 delims=;" %%i in (ou.csv) do (

set "level=%%~j"
if "!level!"=="" (dsadd ou %%~i)

set "level=%%~k"
if "!level!"=="" (dsadd ou "OU=%%~i,OU=%%~j")

set "level=%%~l"
if "!level!"=="" (dsadd ou "OU=%%~i,OU=%%~j,OU=%%~k")

)



This is my container.csv

Directeur;;;
Directeur;Hoofd automatisering;;
Directeur;Hoofd inkoop;;
Directeur;Hoofd aministratie;;
Directeur;Hoofd logistiek;;
Directeur;Verkoop manager;;
Directeur;Marketing & Field Sales manager;;
Directeur;Huishoudelijke dienst /Postverwerking;;
Directeur;MT - assistant;;
Directeur;Afd verkoop buitendiensts;;
Directeur;Hoofd automatisering;Afd. Automatisering;
Directeur;Hoofd inkoop;Assistant inkoop;
Directeur;Hoofd aministratie;Afd. Administratie/facturering;
Directeur;Hoofd logistiek;Hoofd magazijn;
Directeur;Hoofd logistiek;plv hoofd magazijn/logistriek;
Directeur;Verkoop manager;Afd. Verkoop binnendienst;
Directeur;Marketing & Field Sales manager;Afd verkoop buitendienst;
Directeur;Marketing & Field Sales manager;Marketing & Sales Support;
Directeur;Marketing & Field Sales manager;Marketing & Communicatie;
Directeur;Hoofd logistiek;Hoofd magazijn;Magazijnmedewerkers
Directeur;Hoofd logistiek;plv hoofd magazijn/logistriek;Chaffeurs



What am I missing? or what would be a better idea to do?

Also when I manually type dsadd ou "OU=Directeur"
It gives me this error 0x80005000 ("The specified directory service attribute or value does not exist").

But What attribute or value doesn't exists?

Is This A Good Question/Topic? 0
  • +

Replies To: BATCH add OU's in server 2012 r2 using .csv file

#2 RamonRobben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 82
  • View blog
  • Posts: 544
  • Joined: 19-May 14

Re: BATCH add OU's in server 2012 r2 using .csv file

Posted 15 April 2016 - 03:07 AM

So I found out what the 80005000 error was. you got to add DC=Domain,DC=eu
This is my code now it works but its not executing the script it just echoes the script when I run it inside cmd

for /F "tokens=1,2,3,4 delims=;" %%i in (ou.csv) do (

set "level=%%~j"
if "!level!"=="" (dsadd ou "%%~i,DC=monnik-dranken,DC=eu")

set "level=%%~k"
if "!level!"=="" (dsadd ou "OU=%%~i,OU=%%~j,DC=monnik-dranken,DC=eu")

set "level=%%~l"
if "!level!"=="" (dsadd ou "OU=%%~i,OU=%%~j,OU=%%~k,DC=monnik-dranken,DC=eu")

)


Was This Post Helpful? 0
  • +
  • -

#3 RamonRobben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 82
  • View blog
  • Posts: 544
  • Joined: 19-May 14

Re: BATCH add OU's in server 2012 r2 using .csv file

Posted 15 April 2016 - 10:14 AM

So I have been looking around for a while since I posted this question. And I got it working!
Here is the If statement I used:
FOR /f "tokens=%~1 delims=%~2" %%A IN (%~3) DO (

	if "%%~A" NEQ "" IF "%%~B" == "" (
		echo Added OU: %%A
		dsadd ou "OU=%%A,DC=%~4,DC=%~5" >nul
	)


don't pay attention to the %1 - %5 they are just the parameters that I added as an extra in my tool.
I looked for dsadd examples and read some things about it on how to use it.
Also I researched if statements in batch to get a better understanding of how this works.
then the >nul just makes sure that the outputs of that command don't get printed. If you want to disable errors aswell use 2>nul
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • blow up my boots
  • member icon

Reputation: 6440
  • View blog
  • Posts: 26,055
  • Joined: 12-December 12

Re: BATCH add OU's in server 2012 r2 using .csv file

Posted 15 April 2016 - 11:13 AM

Okay, I have to ask, what's an OU?
Was This Post Helpful? 0
  • +
  • -

#5 RamonRobben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 82
  • View blog
  • Posts: 544
  • Joined: 19-May 14

Re: BATCH add OU's in server 2012 r2 using .csv file

Posted 15 April 2016 - 12:06 PM

an OU means Orgazational Unit. You store your network objects in there like groups, users more OU's etc
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1