Oli M
2009-04-02 11:27:02 UTC
Hi Im fairly new to all this
I have put this script together from what I have found on the ent
I have searched for and tried to amend the script myself to include being
able to filter out accounts in a certain security group but to no avail.
Basically I have added all the admin use accounts to a group and want this
script to exclude any accounts in that group from the export results.
I have also searched on getting the last true login stamp and found a couple
of things but really couldn't find a way of adding it into this script.
the last login stamp isn't working as we have more than one dc, and it only
gets the information from the dc the computer you are logged into. How can i
get the information from all the dcs and then display the most recent result
for the last login stamp?
Any help, guidance or pointers would be much appreciated.
Here is the script as it is at the moment
Dim ObjWb
Dim ObjExcel
Dim x, zz
Set objRoot = GetObject("LDAP://RootDSE")
strDNC = objRoot.Get("DefaultNamingContext")
Set objDomain = GetObject("LDAP://" & strDNC) ' Bind to the top of the
Domain using LDAP using ROotDSE
Call ExcelSetup("Sheet1") ' Sub to make Excel Document
x = 1
Call enummembers(objDomain)
Sub enumMembers(objDomain)
On Error Resume Next
Dim Secondary(20) ' Variable to store the Array of 2ndary email alias's
For Each objMember In objDomain ' go through the collection
If ObjMember.Class = "user" then
x = x +1 ' counter used to increment the cells in Excel
objwb.Cells(x, 1).Value = objMember.Class
' I set AD properties to variables so if needed you could do Null
checks or add if/then's to this code
' this was done so the script could be modified easier.
SamAccountName = ObjMember.samAccountName
Cn = ObjMember.CN
Department = objMember.Department
LastLogin = objMember.LastLogin
PasswordLastChanged = objMember.PasswordLastChanged
' Write the values to Excel, using the X counter to increment the
rows.
objwb.Cells(x, 2).Value = SamAccountName
objwb.Cells(x, 3).Value = CN
objwb.Cells(x, 4).Value = Department
objwb.Cells(x, 5).Value = LastLogin
objwb.Cells(x, 6).Value = PasswordLastChanged
' Blank out Variables in case the next object doesn't have a value
for the property
SamAccountName = "-"
Cn = "-"
Department = "-"
LastLogin = "-"
PasswordLastChanged = "_"
End If
' If the AD enumeration runs into an OU object, call the Sub again
to itinerate
If objMember.Class = "organizationalUnit" or OBjMember.Class =
"container" Then
enumMembers (objMember)
End If
Next
End Sub
Sub ExcelSetup(shtName) ' This sub creates an Excel worksheet and adds
Column heads to the 1st row
Set ObjExcel = CreateObject("Excel.Application")
Set objwb = objExcel.Workbooks.Add
Set objwb = objExcel.ActiveWorkbook.Worksheets(shtName)
Objwb.Name = "Active Directory Users" ' name the sheet
objwb.Activate
ObjExcel.Visible = True
objwb.Cells(1, 2).Value = "SamAccountName"
objwb.Cells(1, 3).Value = "CN"
objwb.Cells(1, 4).Value = "Department"
objwb.Cells(1, 5).Value = "LastLogin"
objwb.Cells(1, 6).Value = "PassWordLastChanged"
'formatting for header
Set objRange = objExcel.Range("A1","Z1")
objRange.Interior.ColorIndex = 33
objRange.Font.Bold = True
objRange.Font.Underline = True
End Sub
'autofit the output
Set objRange = objwb.UsedRange
objRange.EntireColumn.Autofit()
ObjExcel.Save("ADoutput.xls")
MsgBox "Done" ' show that script is complete
I have put this script together from what I have found on the ent
I have searched for and tried to amend the script myself to include being
able to filter out accounts in a certain security group but to no avail.
Basically I have added all the admin use accounts to a group and want this
script to exclude any accounts in that group from the export results.
I have also searched on getting the last true login stamp and found a couple
of things but really couldn't find a way of adding it into this script.
the last login stamp isn't working as we have more than one dc, and it only
gets the information from the dc the computer you are logged into. How can i
get the information from all the dcs and then display the most recent result
for the last login stamp?
Any help, guidance or pointers would be much appreciated.
Here is the script as it is at the moment
Dim ObjWb
Dim ObjExcel
Dim x, zz
Set objRoot = GetObject("LDAP://RootDSE")
strDNC = objRoot.Get("DefaultNamingContext")
Set objDomain = GetObject("LDAP://" & strDNC) ' Bind to the top of the
Domain using LDAP using ROotDSE
Call ExcelSetup("Sheet1") ' Sub to make Excel Document
x = 1
Call enummembers(objDomain)
Sub enumMembers(objDomain)
On Error Resume Next
Dim Secondary(20) ' Variable to store the Array of 2ndary email alias's
For Each objMember In objDomain ' go through the collection
If ObjMember.Class = "user" then
x = x +1 ' counter used to increment the cells in Excel
objwb.Cells(x, 1).Value = objMember.Class
' I set AD properties to variables so if needed you could do Null
checks or add if/then's to this code
' this was done so the script could be modified easier.
SamAccountName = ObjMember.samAccountName
Cn = ObjMember.CN
Department = objMember.Department
LastLogin = objMember.LastLogin
PasswordLastChanged = objMember.PasswordLastChanged
' Write the values to Excel, using the X counter to increment the
rows.
objwb.Cells(x, 2).Value = SamAccountName
objwb.Cells(x, 3).Value = CN
objwb.Cells(x, 4).Value = Department
objwb.Cells(x, 5).Value = LastLogin
objwb.Cells(x, 6).Value = PasswordLastChanged
' Blank out Variables in case the next object doesn't have a value
for the property
SamAccountName = "-"
Cn = "-"
Department = "-"
LastLogin = "-"
PasswordLastChanged = "_"
End If
' If the AD enumeration runs into an OU object, call the Sub again
to itinerate
If objMember.Class = "organizationalUnit" or OBjMember.Class =
"container" Then
enumMembers (objMember)
End If
Next
End Sub
Sub ExcelSetup(shtName) ' This sub creates an Excel worksheet and adds
Column heads to the 1st row
Set ObjExcel = CreateObject("Excel.Application")
Set objwb = objExcel.Workbooks.Add
Set objwb = objExcel.ActiveWorkbook.Worksheets(shtName)
Objwb.Name = "Active Directory Users" ' name the sheet
objwb.Activate
ObjExcel.Visible = True
objwb.Cells(1, 2).Value = "SamAccountName"
objwb.Cells(1, 3).Value = "CN"
objwb.Cells(1, 4).Value = "Department"
objwb.Cells(1, 5).Value = "LastLogin"
objwb.Cells(1, 6).Value = "PassWordLastChanged"
'formatting for header
Set objRange = objExcel.Range("A1","Z1")
objRange.Interior.ColorIndex = 33
objRange.Font.Bold = True
objRange.Font.Underline = True
End Sub
'autofit the output
Set objRange = objwb.UsedRange
objRange.EntireColumn.Autofit()
ObjExcel.Save("ADoutput.xls")
MsgBox "Done" ' show that script is complete