VB script ile Domaindeki Userların listelenmesi

Bilişim sektöründe çalışıyorsanız ve de Active Directory konsolu önünüzde açıksa Scripting candır ve de şarttır.Vakit buldukça burda kullandığım scriptleri sizlerle paylaşacağım. Büyük bir Domain yapısında bazen user,computer ve grup işlerinde elle uğraşmak yerine script kullanılması kaçınılmazdır.Mesela domainde bulunan tüm userların çekilmesi gibi.Aşağıdaki kodu notepad aktarıp dosya uzantısını .vbs olarak kaydettiğinizde domaininizde ki tüm userları html rapor olarak alabilirsiniz.

Option Explicit

On Error Resume Next

‘ Declare public variables.

Dim FSO, adsRootDSE, strDomainPath, adsDefaultDomain, strDate, adsUsers, HTMLReport, intCounter

Const ForReading = 1, ForWriting = 2, ForAppending = 8, E_ADS_PROPERTY_NOT_FOUND = &h8000500D

Set FSO = CreateObject(“Scripting.FileSystemObject”)

Set adsRootDSE = GetObject(“LDAP://RootDSE”)

strDomainPath = adsRootDSE.Get(“DefaultNamingContext”)

Set adsDefaultDomain = GetObject(“LDAP://” & strDomainPath)

Set adsRootDSE = Nothing

‘ ** Change these values to reflect your environment.

‘ This is the current date. Change it to meet whatever date format you would like to use.

strDate = Month(Date) & “-” & Day(Date) & “-” & Year(Date) ‘ Creates a date string delimited by hyphens.

‘ Location of user accounts. Default value: (“LDAP://” & strDomainPath) searches the entire domain.

Set adsUsers = GetObject(“LDAP://” & strDomainPath)

‘ The file name for the report.

Set HTMLReport = FSO.OpenTextFile(strDate & “.User-List.html”, ForWriting, true)

adsUsers.Filter = Array(“organizationalUnit”)

‘ Initialize counter.

intCounter = 0

‘ Generate HTML headers.

HTMLReport.WriteLine ( “<html><head>”)

HTMLReport.WriteLine ( “<title>” & strDate & ” – AD User List</title>”)

HTMLReport.WriteLine ( “</head><body>”)

HTMLReport.WriteLine ( “<h2>AD User List – Generated ” & strDate & “</h2>”)

HTMLReport.WriteLine ( “<table><tr><td>Common name</td><td>Given name</td><td>Surname</td></tr>”)

Call EnumOUs(adsUsers)

‘ Close out the report.

HTMLReport.WriteLine ( “<h3>” & intCounter & ” users.</h3></body></html>”)

HTMLReport.Close

‘ Close out objects and quit the script.

Set HTMLReport = Nothing

Set adsUsers = Nothing

Set adsDefaultDomain = Nothing

Set FSO = Nothing

Wscript.Quit

Sub EnumOUs(objParent)

On Error Resume Next

Dim objUser, cn, givenName, surname, objChild

‘ Recursive subroutine to enumerate all OU’s.

objParent.Filter = Array(“User”)

For Each objUser in objParent

If objUser.Class = “user” Then

‘ Expand on this if you would to grab other attributes.

cn = objUser.sAMAccountName

givenName = objUser.givenName

surname = objUser.sn

‘ Generate unique row for user.

HTMLReport.WriteLine ( “<tr><td>” & cn & “</td><td>” & givenName & “</td><td>” & surname & “</td><tr>”)

End If

Next

objParent.Filter = Array(“organizationalUnit”)

For Each objChild In objParent

Call EnumOUs(objChild)

Next

End Sub

Share