Criar usuários no Active Directory usando Planilha do Excel


É muito comum, administradores de rede receberem uma lista com dezenas de usuários novos para serem criados no AD, e geralmente, “pra ontem”.
Para automatizar a criação desses usuários, podemos utilizar um script e uma planilha (XLS) para realizar esta tarefefa em 2 cliques.
A planilha deverá conter as informações dos usuários e o script, irá criar os usuários de acordo com os campos (LDAP attributes) necessários.
Bom, começando pela planilha.
Cada coluna é um atributo e neste caso, o que importa é a ordem dos numeros (1 a 12).



Crie uma planilha com qualquer nome e salve-a em um diretório (Salvei em C:\Scripts como User.xls).
Abaixo, o script VBS:
Option Explicit
Dim objRootLDAP, objContainer, objUser, objShell
Dim objExcel, objSpread, intRow
Dim strUser, strOU, strSheet
Dim strCN, strSam, strFirst, strLast, strPWD, strinitials, stroffice, strmail, strtitle, strdepartment, strprincipalname, strTitulo, strMensagem, strcompany, strdescription, strtelephoneNumber
strOU = “OU=CONTAS NOVAS ,”
strSheet = “C:\scripts\User.xls”
‘ Bind to Active Directory, Users container.
Set objRootLDAP = GetObject(“LDAP://rootDSE”)
Set objContainer = GetObject(“LDAP://” & strOU & objRootLDAP.Get(“defaultNamingContext”))
‘ Open the Excel spreadsheet
Set objExcel = CreateObject(“Excel.Application”)
Set objSpread = objExcel.Workbooks.Open(strSheet)
intRow = 3 ‘Row 1 often contains headings
‘ Here is the ‘DO…Loop’ that cycles through the cells
‘ Note intRow, x must correspond to the column in strSheet
Do Until objExcel.Cells(intRow,1).Value = “”
strSam = Trim(objExcel.Cells(intRow, 1).Value)
strCN = Trim(objExcel.Cells(intRow, 2).Value)
strFirst = Trim(objExcel.Cells(intRow, 3).Value)
strinitials = Trim(objExcel.Cells(intRow, 4).Value)
strLast = Trim(objExcel.Cells(intRow, 5).Value)
strPWD = Trim(objExcel.Cells(intRow, 6).Value)
stroffice = Trim(objExcel.Cells(intRow, 7).Value)
strmail = Trim(objExcel.Cells(intRow, 8).Value)
strprincipalname = strSam & “@teste.interno”
strtitle = Trim(objExcel.Cells(intRow, 9).Value)
strdepartment = Trim(objExcel.Cells(intRow, 10).Value)
strdescription = Trim(objExcel.Cells(intRow, 11).Value)
strtelephoneNumber = Trim(objExcel.Cells(intRow, 12).Value)
strcompany = “Empresa S.A”
‘ Build the actual User from data in strSheet.
Set objUser = objContainer.Create(“User”, “cn=” & strCN)
objUser.sAMAccountName = strSam
objUser.givenName = strFirst
objUser.initials = strinitials
objUser.sn = strLast
objUser.SetInfo
objUser.physicalDeliveryOfficeName = stroffice
objUser.mail = strmail
objUser.userPrincipalName= strprincipalname
objUser.displayName = strCN
objUser.title = strtitle
objUser.department = strdepartment
objUser.company = strcompany
objUser.description = strdescription
objUser.telephoneNumber = strtelephoneNumber
‘ Separate section to enable account with its password
objUser.userAccountControl = 512
objUser.pwdLastSet = 0
objUser.SetPassword strPWD
objUser.SetInfo
intRow = intRow + 1
Loop
objExcel.Quit
strTitulo = “COMANDO CONCLUIDO!!”
strMensagem = _
“USUARIO(S) CRIADO(S) COM SUCESSO!” & vbcrlf & vbcrlf & _
” Mova para OU correta e crie a mailbox.” & vbcrlf & _
“” & vbcrlf & _
“”
‘BtnCode = WshShell.Popup(strMensagem, 5, “Informação:”, 64 + 0)
msgbox strMensagem, 0 + 64, strTitulo
WScript.Quit
Agora, vamos enteder o Script – vou desmembrar as partes mais importantes e comentá-las:
Dim strCN, strSam, strFirst, strLast, strPWD, strinitials, stroffice, strmail, strtitle, strdepartment, strprincipalname, strTitulo, strMensagem, strcompany, strdescription, strtelephoneNumber
Esta parte é onde são declaradas as variáveis, ou seja, os atributos que vou utilizar. Se tiver que acrescentar algum, insira nesta linha (separado por vírgula) ou retire algum campo desnecessário.
strOU = “OU=CONTAS NOVAS ,”
strSheet = “C:\scripts\User.xls”
Bem intuitivo a sintaxe acima. StrOU é em qual Unidade Organizacional os usuários serão criados e strSheet é o caminho da planilha, que também uso para armazenar o script.
strSam = Trim(objExcel.Cells(intRow, 1).Value)
strCN = Trim(objExcel.Cells(intRow, 2).Value)
strFirst = Trim(objExcel.Cells(intRow, 3).Value)
strinitials = Trim(objExcel.Cells(intRow, 4).Value)
strLast = Trim(objExcel.Cells(intRow, 5).Value)
strPWD = Trim(objExcel.Cells(intRow, 6).Value)
stroffice = Trim(objExcel.Cells(intRow, 7).Value)
strmail = Trim(objExcel.Cells(intRow, 8).Value)
strprincipalname = strSam & “@teste.interno”
strtitle = Trim(objExcel.Cells(intRow, 9).Value)
strdepartment = Trim(objExcel.Cells(intRow, 10).Value)
strdescription = Trim(objExcel.Cells(intRow, 11).Value)
strtelephoneNumber = Trim(objExcel.Cells(intRow, 12).Value)
strcompany = “Empresa S.A”
Esta a parte é onde “acontece” as coisas – a parte que exige mais atenção. Como pode ser visto, todas as variáveis estão informadas ai. Observe que cada variavel está vinculada a um número, que no caso, é a coluna da planilha que já foi criada.
Ex: strdepartment = Trim(objExcel.Cells(intRow, 10).Value)
strdepartment é o LDAP Attribute do campo Departamento e intRow, 10 é a coluna 10 da nossa planilha.
Script executado:

Agora segue prints das informações do objeto no AD que definimos:





Espero que este tutorial seja útil a todos, compartilhem, curtam,pois  as informações aqui prestadas são de domínio publico.

Share on Google Plus

About Obarata

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comentários:

Postar um comentário