Page 1 of 1 1
Topic Options
#203109 - 2011-09-20 01:12 PM User import from CSV
Beuz1 Offline
Fresh Scripter

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

I want to add users to my win2003 DC from a CSV using kixtart. Can't seem to find this information on the internet. I found some functions to read a CSV file but don't know how to get te user information from the CSV and add this users to a OU. The OU and username is given in the CSV.

Thanks

Top
#203111 - 2011-09-20 01:25 PM Re: User import from CSV [Re: Beuz1]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11629
Loc: CA
Hello Beuz1 and welcome to the board.

I don't think there is any pre-written code to do exactly what you're asking.

You would probably need to hand code it, but there are hundreds of UDF scripts that can help you to achieve your goals.

http://www.kixtart.org/udf/

Can you post some example data of your CSV (you can use dummy data) so that we can see what you've got so far and what you're trying to do.

Top
#203112 - 2011-09-20 01:36 PM Re: User import from CSV [Re: NTDOC]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
 Code:
for each $userline in csvRead()
 addADUser($userline)
next


just like NTDoc said, give us some pointers where to go with this and you may end up with a script pretty much as simple as my above scriptlet example.
_________________________
!

download KiXnet

Top
#203113 - 2011-09-20 02:04 PM Re: User import from CSV [Re: Lonkero]
Beuz1 Offline
Fresh Scripter

Registered: 2011-09-19
Posts: 8
Loc: Groningen, The Netherlands
Thanks for the quick reply's!
My CSV looks like this:
UserID First name Class (OU)
xxxxxx (numeric) John Doe MB109

If a person doesn't have a class they will be added to
the OU ExStudents.

Top
#203115 - 2011-09-20 03:49 PM Re: User import from CSV [Re: Beuz1]
Beuz1 Offline
Fresh Scripter

Registered: 2011-09-19
Posts: 8
Loc: Groningen, The Netherlands
This is my code so far:

 Code:
Break on

$filename = "test1.csv"

If Open(1,$filename) = 0

 $line = ReadLine(1)

 While @ERROR = 0

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

  ?

  For Each $field In $array
   ? $field
  Next

  $line = ReadLine(1)

 Loop

 $= Close(1)

EndIf

Exit 1 



It just reads my csv and shows it on the screen. But I don't know
how to use this values to create users.

Top
#203117 - 2011-09-20 10:21 PM Re: User import from CSV [Re: Beuz1]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
searching the board returned me this:
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Board=2&Number=188473

and from that a short snippet:
 Code:
$Domain = GetObject("LDAP://cn=Users,dc=us,dc=fabricom,dc=com")
$user = $Domain.Create("user", "cn=" + $Accounts[0,$Element])
$User.Put ("sAMAccountName", $Accounts[1,$Element]) 
$user.SetPassword ($Account[2,$Element])
$user.Description = "Voice Admin"
$user.SetInfo


does that help?
weirdly I couldn't find a ready made UDF for this...
_________________________
!

download KiXnet

Top
#203118 - 2011-09-20 11:58 PM Re: User import from CSV [Re: Lonkero]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4563
Loc: USA
It seems a lot of Active Directory stuff has been overlooked in the UDFs.
Top
#203121 - 2011-09-21 08:37 AM Re: User import from CSV [Re: Allen]
Beuz1 Offline
Fresh Scripter

Registered: 2011-09-19
Posts: 8
Loc: Groningen, The Netherlands
Thanks again!
But I see a lot of code there and I'm pretty new with Kix.
The only thing I need is to put a specific line of a CSV into a variable to create the user with that 4 variables (UserID, First name, Last name and class). When I have that variables from the CSV it can't be difficult to do the rest.

Top
#203123 - 2011-09-21 02:48 PM Re: User import from CSV [Re: Beuz1]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
If this is really "CSV" format and you simply split on "," you will not obtain a valid set of data, as commas could be embedded in your data. Using one of the CSV UDFs will prevent this. The CSV() UDF that I wrote converts a CSV string to an array, or an array to a CSV string with proper formatting for embedded quotes and commas. If you tightly control your data and can insure that no special chars (quotes, commas, etc) are part of the data fields, then a simple split will work. I think you're asking for trouble assuming that you'll never have special chars in your data, though.

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

Top
#203124 - 2011-09-21 02:55 PM Re: User import from CSV [Re: Allen]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Allen:

FYI - AD-related UDFs from my library include:
 Code:
AdCreateGroup()		Creates an active directory group 
AdGroupMember()		List or Modify an AD Group's contents 
ADSIUserInfo()		Get or Set a user attribute via ADSI
ADSIUserStatus		Returns the status of a user account 
ComputerInGroup()		Checks the local computer for membership in one or more groups
GetMachineList()		Returns a list of all computers in a specified domain
GetObjectOU()		Returns the OU string for the current user (default) or computer
GetResourceBySrv()		Returns an array of SRV record data 
GetUserOU()		Returns the OU string for the current user 
GroupMember()		List or Modify a Group's contents 
GroupMembers()		Returns an array of all group members of the specified group 
InOU()			Returns a boolean if the current user or computer is a member of the named OU
LDAPQuery()		Uses ADODB to retrieve information from Active Directory.
ListNestedGroups()		Retrieves a list of all the groups an account is a member of including nested groups
SetDnsSRV()		Creates a DNS SRV record (with defaults for SWDIST)
All of these are available from the Resources / Kix UDF Lib page on my web site, and most are posted here on Korg.

There are likely several more on KORG, but these are the ones I use the most. The SetDnsSrv and GetResourceBySrv are specifically DNS UDFs but play heavily into AD for locating (and defining) resources within the network. I also have some dedicated code that I never generalized into UDFs for user management - I might take a look at those when I have time (time? what's that?).

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

Top
#203219 - 2011-10-10 02:57 PM Re: User import from CSV [Re: Glenn Barnas]
Beuz1 Offline
Fresh Scripter

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

 Code:
Break on

$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("TestServer.local",
                  "cn= Users",
                  $array[0],
                  $array[1],
                  $array[2],
                  "Dit is een user die is aangemaakt door het script van Jeroen Beuzenberg",
                  $array[4],
                  "http://www.jeroenbeuzenberg.test.nl",
			$array[1])

Function CreateUserAccount($Domain,
                           $ContainerDN,
                           $Account,
                           $FirstName,
                           $LastName,
                           optional $Description,
                           optional $lokaal,
                           optional $WebPage,
				   $password,
                           optional $userPrincipalName )

    Dim $aDom, $part, $sDNdom, $LDAPpath, $oContainer, $oUser

    if VarTypeName($userPrincipalName) = "Empty"
        $userPrincipalName = $Account + "@@" + $Domain
    endif

    $aDom = split($Domain,".")
    for each $part in $aDom
	    $sDNdom = "" + $sDNdom + ",dc=" + $part
    next

    $LDAPpath = "LDAP://" + $Domain + "/" + $ContainerDN + $sDNdom
    ;? $LDAPpath

    $oContainer = GetObject($LDAPpath)
    if @error
        ? "GetObject Error: " + @error +  " "  + @Serror
    endif

    $oUser = $oContainer.Create("User", "cn=" + $Account)
    if @error
        ? "Create Error: " + @error +  " "  + @Serror
    endif

    $oUser.Put("sAMAccountName", $Account)
    $oUser.Put("givenName", $FirstName)     
    $oUser.Put("sn", $LastName)             
    $oUser.Put("displayName",$LastName  + iif($LastName, ", ","") +
                             $FirstName + iif($MiddleInitial, " ", "") +
                             $MiddleInitial)
    $oUser.Put("description", $Description) 
    $oUser.Put("physicalDeliveryOfficeName",$lokaal)
    $oUser.Put("wWWHomePage",$WebPage)      
    $oUser.Put("userPrincipalName", $userPrincipalName)

    $oUser.SetInfo
    $oUser.AccountDisabled = 0
    $oUser.SetPassword($password)
    $oUser.SetInfo

    $CreateUserAccount = @ERROR
    if $CreateUserAccount
        ? "SetInfo Error: "" Deze gebruiker bestaat al! "
	  ? "De gebruiker [" + $array[0] + "] die u probeerde toe te voegen is niet gewijzigd."
	  ? "Foutcode: "
    endif
Endfunction
  

  $line = ReadLine(1)
  
 Loop

 $= Close(1)

Top
#203221 - 2011-10-10 05:06 PM Re: User import from CSV [Re: Beuz1]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
cool
_________________________
!

download KiXnet

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 235 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.076 seconds in which 0.038 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