Welcome to Scripting Forums Sign in | Join | Help
in Search

List Active Directory group members in Excel

Last post 10-24-2007, 1:31 PM by pearlr. 2 replies.
Sort Posts: Previous Next
  •  10-15-2007, 10:58 AM 28653

    List Active Directory group members in Excel

    Hello,

    I use the VBScript under WSH 5.6 below to attempt to list membership of all of the groups in an AD tree. The output is in Excel and seems to work quite well until the script hits a group with multiple members. The script will then abend with the error "(224, 3) Microsoft Office Excel: Sort method of Range class failed" at line 224 which is using the Sort method in order to attempt to sort the accounts by last name, then first name. Does anyone have an idea as to why the sort fails? If I comment out the sort, the script completes properly.

     All help will be greatly appreciated.

     VBScript follows:

    Option Explicit

    Const ADS_SCOPE_SUBTREE = 2
    Const UF_ACCOUNTDISABLE = &H2

    Dim rootDSE
    Dim strRootDSE
    Dim strCommand
    Dim strGroupObj
    Dim strMember
    Dim strMemberFwd
    Dim objExcel
    Dim objConnection
    Dim objCommand
    Dim objRecordSet
    Dim objGroup
    Dim objMember
    Dim objRange
    Dim objRange2
    Dim objRange3
    Dim objMemberFwd
    Dim arrMemberOf
    Dim sh
    Dim i
    Dim k
    Dim g
    Dim x

    'connect to the root of AD
    Set rootDSE = GetObject("LDAP://RootDSE")
    strRootDSE = rootDSE.Get("defaultNamingContext")
    strCommand = "SELECT cn, distinguishedName, objectCategory, SN, givenName FROM 'LDAP://" & strRootDSE & "' WHERE objectCategory='group' AND cn = '*' AND member = '*'" 
    'WScript.Echo strRootDSE
    'WScript.Echo strCommand

    Set objExcel = CreateObject("Excel.Application")

    objExcel.Visible = True
    objExcel.Workbooks.Add

    For Each sh In objExcel.Sheets
     i = i + 1
    Next

    For k = 1 To i-1
     objExcel.ActiveWorkbook.Sheets(1).Delete
    Next

    Set objConnection = CreateObject("ADODB.Connection")
    Set objCommand =   CreateObject("ADODB.Command")
    objConnection.Provider = "ADsDSOObject"
    objConnection.Open "Active Directory Provider"
    Set objCommand.ActiveConnection = objConnection

    objCommand.Properties("Page Size") = 1000
    objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
    objCommand.Properties("Sort on") = "cn"

    objCommand.CommandText = strCommand

    Set objRecordSet = objCommand.Execute
    objRecordSet.MoveFirst
    g = 1

    Do Until objRecordSet.EOF
        Call NewSheet
        objRecordSet.MoveNext
     g = g + 1
    Loop

    Wscript.Quit

    Sub NewSheet
     strGroupObj = "LDAP://" & objRecordSet.Fields("distinguishedName").Value
     WScript.Echo "DN = " & objRecordSet.Fields("distinguishedName").Value
     WScript.Echo "strGroupObj = " & strGroupObj
     
    ' On Error Resume Next
     Set objGroup = GetObject(strGroupObj)
     
    ' On Error Goto 0
     
     objGroup.GetInfo
     
    ' On Error Resume Next
     arrMemberOf = objGroup.GetEx("member")

     If Err.Number = 0 Then
      If g > 1 Then
       objExcel.Worksheets.Add
      Else
       objExcel.ActiveWorkbook.Sheets(g).Select()
      End If
     
      If Len(objRecordSet.Fields("cn").Value) <= 31 Then
       objExcel.ActiveSheet.Name = objRecordSet.Fields("cn").Value
      Else
       objExcel.ActiveSheet.Name = Left(objRecordSet.Fields("cn").Value,31)
      End If
     
      objExcel.Cells(1, 1).Value = "Last name"
      objExcel.Cells(1, 2).Value = "First name"
      objExcel.Cells(1, 3).Value = "Department"
      objExcel.Cells(1, 4).Value = "Phone number"
      objExcel.Cells(1, 5).Value = "samAccountName"
      objExcel.Cells(1, 6).Value = "Default SMTP Address"
      objExcel.Cells(1, 7).Value = "Forwarding SMTP Address"
      objExcel.Cells(1, 8).Value = "Keep emails in server Inbox"
      objExcel.Cells(1, 9).Value = "Status"
      objExcel.Cells(1, 10).Value = strGroupObj
      objExcel.Cells(1, 1).EntireRow.Font.Bold = True
      objExcel.Cells(1, 1).EntireRow.Font.Underline = True
      objExcel.Cells(1, 1).EntireRow.Font.ColorIndex = 10

      x=2
     
      For Each strMember in arrMemberOf
       Set objMember = GetObject("LDAP://" & strMember)
          objExcel.Cells(x, 1).Value = _
              objMember.SN
          objExcel.Cells(x, 2).Value = _
              objMember.givenName
          objExcel.Cells(x, 3).Value = _
              objMember.department
          objExcel.Cells(x, 4).Value = _
              objMember.telephoneNumber
          objExcel.Cells(x, 5).Value = _
              objMember.samAccountName
          objExcel.Cells(x, 6).Value = _
              objMember.mail

    '   WScript.Echo objMember.Class
    '   WScript.Echo objMember.userflags

    '   If objMember.Class = "user" Then
    '    If objMember.UserFlags And UF_ACCOUNTDISABLE Then
    '     objExcel.Cells(x, 9).Value = _
    '      "Diasbled"
    '    Else
    '     objExcel.Cells(x, 9).Value = _
    '      "Enabled"
    '    End If
    '   End If
       
       'On Error Resume Next
    '   WScript.Echo(objMember.altRecipient)
       
    '   If (objMember.altRecipient) Then
    '    strMemberFwd = objMember.altRecipient
    '    WScript.Echo(objMember.altRecipient)
    '    Set objMemberFwd = GetObject("LDAP://" & objMember.altRecipient)
    '       objExcel.Cells(x, 7).Value = _
    '           objMemberFwd.mail

    '    If objMember.deliverAndRedirect = True Then
    '     objExcel.Cells(x, 8).Value = _
    '            "Yes"
    '    Else
    '     objExcel.Cells(x, 8).Value = _
    '            "No"
    '          End If
    '         End If

       On Error GoTo 0

          x = x + 1
      Next

      Set objRange = objExcel.Range("A1")
      objRange.Activate
      Set objRange = objExcel.ActiveCell.EntireColumn
      objRange.Autofit()

      Set objRange = objExcel.Range("B1")
      objRange.Activate
      Set objRange = objExcel.ActiveCell.EntireColumn
      objRange.Autofit()

      Set objRange = objExcel.Range("C1")
      objRange.Activate
      Set objRange = objExcel.ActiveCell.EntireColumn
      objRange.Autofit()

      Set objRange = objExcel.Range("D1")
      objRange.Activate
      Set objRange = objExcel.ActiveCell.EntireColumn
      objRange.Autofit()

      Set objRange = objExcel.Range("E1")
      objRange.Activate
      Set objRange = objExcel.ActiveCell.EntireColumn
      objRange.Autofit()

      Set objRange = objExcel.Range("F1")
      objRange.Activate
      Set objRange = objExcel.ActiveCell.EntireColumn
      objRange.Autofit()

      Set objRange = objExcel.Range("G1")
      objRange.Activate
      Set objRange = objExcel.ActiveCell.EntireColumn
      objRange.Autofit()

      Set objRange = objExcel.Range("H1")
      objRange.Activate
      Set objRange = objExcel.ActiveCell.EntireColumn
      objRange.Autofit()

      Set objRange = objExcel.Range("I1")
      objRange.Activate
      Set objRange = objExcel.ActiveCell.EntireColumn
      objRange.Autofit()

      Set objRange = objExcel.Range("J1")
      objRange.Activate
      Set objRange = objExcel.ActiveCell.EntireColumn
      objRange.Autofit()

      Set objRange = objExcel.Range("A1").SpecialCells(11)
      objRange.Activate
      Set objRange2 = objExcel.Range("A1")
      Set objRange3 = objExcel.Range("B1")
    '  objRange.Sort objRange2,1,objRange3,,1,,,1
      objRange.Sort objRange2,1,objRange3,,1,,,1

     Else
      g = g -1
     End If

     On Error GoTo 0
      
    End Sub

     

  •  10-19-2007, 1:21 PM 28700 in reply to 28653

    Re: List Active Directory group members in Excel

    After I removed .SpecialCells(11) from the line Set objRange = objExcel.Range("A1").SpecialCells(11) the script ran without generating that specific error.  However I do get a different error much later on which you may or may not be able to recreate depending on your AD structure.

    C:\Scripts\listgroups.vbs(98, 4) Microsoft Office Excel: Cannot rename a sheet to the same name as another sheet, a referenced object library or a workbook referenced by Visual Basic.

    Steve

  •  10-24-2007, 1:31 PM 28728 in reply to 28700

    Re: List Active Directory group members in Excel

    Steve,

    Thanks for your reply. I was able to resolve the sort issue on my own shortly after I made this post. I did run into the issue that you mention, but was able to get around it by using the Scripting Dictionary Object to build a table of contents first and adjusting the name for the spreadsheet tab there. I then use that dictionary object's keys and items to generate a "Table of Contents" sheet in the work book to match the group's DN to the spreadsheet tab.

     Richard

View as RSS news feed in XML
SPONSORED LINKS FEATURED LINKS

Free Download –VS 2008 TrainingExperts Ken Getz & Robert Green plus labs, code, courseware Maximize speed, performance and reliablity of your PCs and servers—automatically!Speed Up Your PC! Try Diskeeper 2008 with InvisiTasking Free Now! Register for SolarWinds VM MonitorGet X-Ray Vision into Your ESX Servers with SolarWinds FREE VM Monitor GoGrid Offers FREE Trial for Windows Cloud ServersDeploy Windows Server 2003 and 2008 with free load balancing through GoGrid’s award winning web-based GUI – all in less than 5 minutes Order Your SQL Fundamentals CD Today! Learn how to use SQL Server, understand Office integration techniques and dive into the essentials of SQL Express and Visual Basic with this free SQL Fundamentals CD. How healthy is your Exchange Server? Find out Now!Automatic Exchange Server Maintenance helps prevent disasters and improves performance. Download a FREE Exchange Server analysis tool. You've Deployed SharePoint...Now What?This one-day free online conference delivers the technical knowledge needed to kick MOSS up a notch. In one information-packed day, independent SharePoint experts will present practical, real-world information and provide take-away, ready-to-use solutions Ease Your Scripting Pains with the Flexibility of PowerShell!Paul Robichaux equips you with PowerShell basics in 3 introductory lessons, each followed by live Q&A—all on your own computer! Register today! What Would You Do If You Ran Microsoft?ITTV's 2008 inaugural video contest, "If I Ran Microsoft..." is your chance to tell it like it is. Be goofy or be serious, but don"t miss this chance to have fun, win prizes, and go viral in a major way. Maximize Your SharePoint InvestmentThis web seminar discusses how true bi-directional replication of SharePoint content from one server to another enables branch offices to maintain access to current SharePoint content. Rock Your Knowledge, and Compete with Friends and Colleagues!Are you the Web Application Performance Guru in your office? It's time to have fun! Download now to access the crossword puzzle. Challenge yourself and complete this fun activity!

 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing