开发者

Import users into Active Directory from CSV/Excel

开发者 https://www.devze.com 2023-04-11 00:19 出处:网络
How can i import a set of users into an Active Directory organizational unit (the only one my user has access to). I want to be able to import the users and passwords from a CSV or Excel开发者_开发知识

How can i import a set of users into an Active Directory organizational unit (the only one my user has access to). I want to be able to import the users and passwords from a CSV or Excel开发者_开发知识库 file as well as setting the account and password not to expire.

Can it be done?


If you can use PowerShell here is the begining of a script :

# Imput of the password 
$defMDP = Read-Host "Entrez le mot de passe par défaut : " #-asSecureString
$utilisateurs = Import-csv c:\pgdvlp\MesUtilisateurs.csv

foreach ($utilisateur in $utilisateurs)
{
  # Create an AD connexion
  $urlLDAP = "LDAP://"+ $utilisateur.silLDAPA + "/" + $utilisateur.silOU + "," + $utilisateur.silLDAPB
  $objOUCreation = [ADSI] $urlLDAP

  # Create the user object
  $strUtilisateur = $utilisateur.silPrenom + " " + $utilisateur.silNom
  $objUtilisateur = $objOUCreation.create("inetOrgPerson", "cn=" + $strUtilisateur)
  $objUtilisateur.SetInfo()

  # Put the account parameters
  $strLogin = $utilisateur.silPrenom[0] + $utilisateur.silNom
  $objUtilisateur.samaccountname = $strLogin
  $objUtilisateur.givenName = $utilisateur.silPrenom
  $objUtilisateur.sn = $utilisateur.silNom
  $objUtilisateur.displayName = $utilisateur.silPrenom + " " + $utilisateur.silNom
  $objUtilisateur.userPrincipalName = $strLogin + "@" + $utilisateur.silDomaine
  $objUtilisateur.company = $utilisateur.silSociete
  $objUtilisateur.department = $utilisateur.silDepartment
  $objUtilisateur.title = $utilisateur.silTitre
  $objUtilisateur.mail = $utilisateur.silAddresseMail
  $objUtilisateur.telephoneNumber = $utilisateur.silNumTel

  # put the de password
  $objUtilisateur.SetPassword($defMDP)
  $objUtilisateur.pwdLastSet = 0

  # put the account state
  $objUtilisateur.userAccountControl = $utilisateur.silEtatCompte

  # write the datas
  $objUtilisateur.SetInfo()
}

Here is something in VBScript (sorry for the vars name in french) :

'==========================================================================
'
' NAME: CREATEUSER.VBS
'
' AUTHOR: JPB , Silogix
' DATE  : 29/06/2009
'
' COMMENT: 
'
'==========================================================================
Option Explicit
On Error Resume Next

'==========================================================================
' User creation
'==========================================================================
Const UF_NORMAL_ACCOUNT = 512 '0x0200
Function createUser(ou, nom, prenom, telephone, domaine)
  On Error Resume Next
  Dim L1 ' Première lettre du prénom
  Dim Login ' Identifiant de connexion
  Dim EMail ' Adresse de courrier
  Dim commonName
  Dim Password
  Dim newUser
  Dim mail
  Dim strwhereToCreate
  Dim ouWhereToCreate

  commonName = "CN=" & prenom & " " & nom
  Password = "test.2011"
  L1 = Mid(prenom, 1, 1)
  Login = L1&"."&nom
  mail = Login & "@" & domaine  
  Err.Clear
  set newUser = ou.Create ("user", commonName)
  newUser.put "userPrincipalName", mail
  newUser.put "samAccountName", Login
  newUser.SetInfo
  If (Err.number <> 0) Then
    createUser = Err.number 
    Exit function
  End If

  newUser.put "userAccountControl", UF_NORMAL_ACCOUNT
  newUser.put "telephoneNumber", telephone
  newUser.SetPassword Password
  newUser.SetInfo

  createUser = 0

End Function

'==========================================================================
' Test of command line arguments
'==========================================================================
If WScript.Arguments.Count <> 1 Then
  WScript.Echo "Attention : Il faut au moins un paramètre"
  WScript.Echo "Exemple : CreateUser.vbs c:\temp\users.csv"
  WScript.Quit(1)
End If

Dim machine
Dim oRootDSE ' Root Directory Service Specific Entry
Dim ofso ' Acces to file system
Dim oUsersFile ' The user file
Dim oADSI ' Acces to ADSI
Dim strwhereToCreate ' Target OU string
Dim ouWhereToCreate ' Target OU object

set ofso = CreateObject("Scripting.FileSystemObject")
' Does file exists
If ofso.FileExists(WScript.Arguments(0)) = False Then
  WScript.Echo Err.Description & " : " &  WScript.Arguments(0)
  WScript.Quit(2)  
End If

' AD Connexion
machine = "192.168.183.138"
Set oRootDSE = GetObject("LDAP://" & machine & "/RootDSE")
strwhereToCreate = "ou=OU," & oRootDSE.get("defaultNamingContext")
Set oADSI = GetObject("LDAP:")
Set ouWhereToCreate = oADSI.openDsObject("LDAP://" & machine & "/"&strwhereToCreate, "societe\administrateur", "test.2011", 1)

Set oUsersFile = ofso.OpenTextFile(WScript.Arguments(0))
Do While Not oUsersFile.AtEndOfStream
  Dim arrArgUser
  Dim strLine 

  strLine = oUsersFile.ReadLine
  arrArgUser = split(strLine,";")
  If arrArgUser(0) <> "Prenom" Then
    Dim Rc
    Rc = 0
    Rc = createUser(ouWhereToCreate, arrArgUser(1), arrArgUser(0), arrArgUser(4), "societe.fr")  
    If Rc <> 0 Then
      WScript.Echo "Impossible de Créér " & arrArgUser(0) & " " & arrArgUser(1) & " erreur " & Rc
    End If
  End If

Loop
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号