1. Este site usa cookies. Ao continuar a usar este site está a concordar com o nosso uso de cookies. Saber Mais.

[ajuda] vb script para criar user no AD

Discussão em 'Programação' iniciada por motchatchu, 27 de Janeiro de 2009. (Respostas: 0; Visualizações: 1142)

  1. motchatchu

    motchatchu Power Member

    boas tenho o seguinte script:
    Código:
    Dim sCSVFileLocation
    Dim sCSVFile
    Dim oConnection
    Dim oRecordSet
    Dim oNewUser
     
    ' Variables needed for LDAP connection
    Dim oRootLDAP
    Dim oContainer
     
    ' Holding variables for information import from CSV file
    Dim sLogon
    Dim sFirstName
    Dim sLastName
    Dim sDisplayName
    Dim sPassword
    Dim nPwdLastSet
    Dim nUserAccountControl ' Used to enable the account
    Dim sDomain
     
    ' Modify this to match your company's AD domain
    sDomain="cyber.pap.pt"
     
    ' Input file location
    sCSVFileLocation = "D:\Scripts\" 'KEEP TRAILING SLASH!
     
    ' Full path to input file
    sCSVFile = sCSVFileLocation&"Book2.csv"
     
    ' Commands used to open the CSV file and select all of the records
    set oConnection = createobject("adodb.connection")
    set oRecordSet = createobject("adodb.recordset")
    oConnection.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & sCSVFileLocation & ";Extended Properties=""text;HDR=NO;FMT=Delimited"""
    oRecordSet.open "SELECT * FROM " & sCSVFile ,oConnection
     
    ' Create a connection to the Active Directory Users container.
    Set oRootLDAP = GetObject("[URL]ldap://rootDSE[/URL]")
    Set oContainer = GetObject("[URL]ldap://cn=Users[/URL]," & _
    oRootLDAP.Get("defaultNamingContext")) 
     
    ' Allows processing to continue even if an error occurs (i.e. dup user)
    ' We put this below the CSV and AD information since processing can
    ' continue with a single bad record, but not if there is a problem with
    ' the CSV file or AD connection
    on error resume next 
     
     
    ' Reads the values (cells) in the sInputFile file.
    Err.clear' --------- Start creating user account
    ' Read variable information from the CSV file
    ' and build everything needed to create the account
    sLogon = oRecordSet.Fields.Item(0).value
    sFirstName = oRecordSet.Fields.Item(1).value
    sLastName = oRecordSet.Fields.Item(2).value
    sDisplayName = sLastName&", "&sFirstName
    sPassword = oRecordSet.Fields.Item(3).value
    ' Build the User account
    Set oNewUser = oContainer.Create("User","cn="&sFirstName&" "&SLastName)
    oNewUser.put "sAMAccountName",lcase(sLogon)
    oNewUser.put "givenName",sFirstName
    oNewUser.put "sn",sLastName
    oNewUser.put "UserPrincipalName",lcase(SLogon)&"@"&sDomain
    oNewUser.put "DisplayName",sDisplayName
    oNewUser.put "name",lcase(sLogon)
     
    ' Write this information into Active Directory so we can
    ' modify the password and enable the user account
    oNewUser.SetInfo
     
    ' Change the users password
    oNewUser.SetPassword sPassword
    oNewUser.Put "pwdLastSet", 0
    ' Enable the user account
    ' oNewUser.Put "userAccountControl", 512
    oNewUser.AccountDisabled = FALSE
    oNewUser.SetInfo
    ' Used only for debugging
    'if err.number = -2147019886 then
    ' msgbox "User logon " & sLogon & "already exists"
    'End If
    oinputrecordset.movenext
    Loop
     
    isto serve para correr no windows server e adiciona um utilizador no active directory apartir de um fixeiro .csv
    Ocorre qe ele cria o utilizador activo e muito bem, mas cria sem password!
     
    Última edição pelo moderador: 28 de Janeiro de 2009

Partilhar esta Página