Arquivo da tag: Active Directory

VBScript – Importar Lista de Usuários do Active Directory para Planilha Excel

  1. Abra o bloco de notas
  2. Copie o Script e cole no bloco de notas
  3. Salve o Arquivo com o nome: ImportaUsuariosAD.vbs
  4. Execute ImportaUsuariosAD.vbs, ele ira abrir o Excel e ira adicionar todos os usuários do AD.

‘ ———————————–Copie Aqui———————————————————–
Set objCn = CreateObject(“ADODB.Connection”)
Set objRs = CreateObject(“ADODB.Recordset”)
Set objNetwork = CreateObject(“WScript.Network”)
Set objExcel = CreateObject(“Excel.Application”)

objExcel.Visible = True
objExcel.Workbooks.Add
objExcel.Cells(1, 1).Value = “Usuario de rede”
objExcel.Cells(1, 2).Value = “Nome Completo”
objExcel.Cells(1, 3).Value = “Email”
strDomain = objNetwork.UserDomain
intRow = 2

Set DomObj = GetObject(“WinNT://” & strDomain )
DomObj.Filter = Array(“User”)

objExcel.Range(“A1:C1″).Select
objExcel.Selection.Interior.ColorIndex = 10
objExcel.Selection.Font.ColorIndex = 11
objExcel.Selection.Font.Bold = True
objExcel.Cells.EntireColumn.AutoFit

For Each objUser In DomObj
UserPath = GetUserPath(objUser.Name)
Set objUserLDAP = getobject(UserPath)
If Trim(Cstr(“” & objUserLDAP.mail)) <> “” Then
objExcel.Cells(intRow, 1).Value = objUserLDAP.sAMAccountName
objExcel.Cells(intRow, 2).Value = objUserLDAP.DisplayName
objExcel.Cells(intRow, 3).Value = objUserLDAP.mail
intRow = intRow + 1
End If
objExcel.Cells.EntireColumn.AutoFit
Next

objExcel.Cells.EntireColumn.AutoFit

Set objCn = Nothing

MsgBox “Dados importados do AD para Planilha Excel com Sucesso!!!”

Function getUserPath(byval sUserName)
set cmd=createobject(“ADODB.Command”)
set cn=createobject(“ADODB.Connection”)
set rs=createobject(“ADODB.Recordset”)
cn.open “Provider=ADsDSOObject;”
cmd.commandtext = “SELECT adspath from ‘LDAP://” & getnc & _
“‘ WHERE objectCategory = ‘User’ and sAMAccountName = ‘” & sUserName & “‘”
cmd.activeconnection = cn
set rs = cmd.execute
if rs.bof <> true and rs.eof<>true then
getUserPath=rs(0)
else
getUserPath = “”
end if
cn.close
end function

function getNC
set objRoot=getobject(“LDAP://RootDSE”)
getNC=objRoot.get(“defaultNamingContext”)
end function
‘ ———————————–Até Aqui———————————————————–