Page 1 of 1 1
Topic Options
#203723 - 2011-11-15 02:16 PM Move Users to specified OU
Beuz1 Offline
Fresh Scripter

Registered: 2011-09-19
Posts: 8
Loc: Groningen, The Netherlands
Hello,

This is what I want:

1. Move users from multiple organizational units to one (Old Student) and disable them.
2. Check if users from Old Student are in the CSV, if so move them to the OU specified in the CSV.
3. Add all the other users from the CSV that where not in the OU Old Student.

This way all the students that where in the AD but not in the CSV will be in one OU and disabled.

I allready have step 3 and i have a script that lets me put all the user attributes in to arrays.

 Code:
Break on
If InGroup ("Users,dc=Testserver,dc=local",
		Move "ou=Oud Leerling,dc=Testserver,dc=local")

$filename = "test1.csv"

If Open(1,$filename) = 0

 $line = ReadLine(1)

 While @ERROR = 0

  $array = Split($line,",")

 ?
? "De volgende gebruiker wordt toegevoegd:"
? $array[0] + " uit lokaal " + $array[4] + "."
CreateUserAccount("ou="+$array[4]+",dc=TestServer,dc=local",
                  $array[3],
		      $array[3],
                  $array[1],
                  $array[2],
                  $array[0],
                  $array[3],
                  $array[4],
			$array[4])
			
			Function CreateUserAccount($dc,
							   $sAMAccountName,
							   $UserName,
							   $FirstName,
							   $LastName,
							   $DisplayName, 
							   $Passwd,
							   $Group,
							   $Resident )

;-------------- Create User Account ------------------------------------

$adsOU = GetObject("LDAP://" + $dc)

If @ERROR<>0
	? "ERROR: Cannot access OU"
	;? $dc
	? @SERROR + " (" + @ERROR + ")"
	Quit 1
EndIf

$adsUser = $adsOU.Create("user", "cn=" + $UserName)
$adsUser.put("sAMAccountName", $sAMAccountName)
$adsUser.Put("DisplayName", $DisplayName)
$adsUser.Put("ProfilePath", "\\Server\Profiles\" + $Group + "\" + $UserName)
$adsUser.SetInfo

$adsUser = GetObject("LDAP://cn=" + $UserName + "," + $dc)

$adsUser.GetInfo
$adsUser.SetPassword($Passwd)
$adsUser.AccountDisabled = 0
$adsUser.IsAccountLocked = 0
$adsUser.SetInfo

EndFunction
  

  $line = ReadLine(1)
  
 Loop

 $= Close(1)

Top
#203724 - 2011-11-15 03:10 PM Re: Move Users to specified OU [Re: Beuz1]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Take a look at the CSV UDF, which converts CSV Data to an array (and vice-versa). It handles things like embedded commas in your data, which your "Split($data, ',')" will not.

Also, I'd read the entire file into an array (See FileIO and other UDFs for this) so that you can quickly scan the entire array for the student info via AScan().

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#203744 - 2011-11-16 08:25 AM Re: Move Users to specified OU [Re: Glenn Barnas]
Beuz1 Offline
Fresh Scripter

Registered: 2011-09-19
Posts: 8
Loc: Groningen, The Netherlands
Thanks for your reply. Will do that but first I want to move users from one ou to another. After I have done that I will take a look at the CSV UDF. Can't find anything about moving a user. Only in VB.
Top
#203746 - 2011-11-16 04:28 PM Re: Move Users to specified OU [Re: Beuz1]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
From what I've seen in vbscripts, moving a user is pretty simple...Here is one of the example scripts converted to kix.

 Code:
Dim $objNewOU,$objOldOU,$filter[0],$objUser

$objNewOU = GetObject("LDAP://ou=finance,dc=fabrikam,dc=com")
$objOldOU = GetObject("LDAP://ou=accounting,dc=fabrikam,dc=com")

$filter[0] = "User"
$objOldOU.Filter = $filter

For Each $objUser in $objOldOU
   If $objUser.class = "User"
       $objNewOU.MoveHere($objUser.ADsPath,"")
   Endif
Next

Top
#203775 - 2011-11-17 09:44 AM Re: Move Users to specified OU [Re: ShaneEP]
Beuz1 Offline
Fresh Scripter

Registered: 2011-09-19
Posts: 8
Loc: Groningen, The Netherlands
Thanks. I just tried that one. Looks like this with my LDAP data:

DIM $objNewOU,$objOldOU,$filter[0],$objUser

$objNewOU = GetObject("LDAP://ou=Oud Leerling,dc=TestServer,dc=local")
$objOldOU = GetObject("LDAP://ou=108,dc=TestServer,dc=local")

$filter[0] = "User"
$objOldOU.Filter = $filter

For Each $objUser In $objOldOU
If $objUser.class = "User"
$objNewOU.MoveHere($objUser.ADsPath,"")
EndIf
Next

So theres a user in 108 that needs to be moved to "Oud Leerling".
When I run the script it gets executed but the user is still in 108.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 525 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.063 seconds in which 0.032 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org