Questions

export to excel the list of members in a specific security group thru VBS

Tags:
+
0 Votes
Locked

export to excel the list of members in a specific security group thru VBS

rajesh.chilagani
VB Script help in collecting the Data from AD Security group
  • +
    0 Votes
    robo_dev

    so you need to do a LDAP query using VB

    http://techtasks.com/code/viewbookcode/1525

    +
    0 Votes
    Rabbit_Runner

    'Script created on 5 December 2007
    'Prompts user to enter friendly name of a group and then dumps
    'the display names of the members of that group to both screen and
    'a text file on the root of Where the vbs file resides.

    '******************************DECLARATIONS**********************************
    Const ADS_SCOPE_SUBTREE = 2
    Const ADS_CHASE_REFERRALS_SUBORDINATE = &H20
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set DataList = CreateObject("System.Collections.ArrayList")

    dim sCurPath
    sCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".") & "\"
    'WScript.Echo sCurPath


    strGroupName = GetGroup
    'WScript.Echo GetGroup


    '******************************FUNCTIONS**********************************
    Function Output(group)
    group.GetInfo
    arrMemberOf = group.GetEx("member")
    For Each strMember in arrMemberOf
    strLDAPPath = "LDAP://" & strMember
    Set objUser = GetObject(strLDAPPath)
    DataList.Add objUser.DisplayName
    Next
    DataList.Sort()
    'strOutputPath = "c:\" & group.DisplayName & ".txt"
    strOutputPath = sCurPath & right(group.name,(len(group.name)-3)) & ".txt"
    wscript.echo strOutputPath
    Set objOutput = objFSO.CreateTextFile(strOutputPath)
    WScript.Echo "Members of " & group.name
    For Each strItem in DataList
    intTotalMembers = intTotalMembers + 1
    objOutput.WriteLine strItem
    WScript.Echo strItem
    Next
    WScript.Echo
    WScript.Echo intTotalMembers & " total members of " & group.Name & "."
    objFinal = MsgBox("File has been saved as " & strOutputPath,vbOKOnly,"File Information")
    End Function

    Function GetGroup
    strGroup = InputBox("Enter the group name.","Enter Name")
    If strGroup = "" Then
    WScript.Quit
    Else
    Main(strGroup)
    End If
    End Function

    Function Main(group)
    On Error Resume 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("Chase referrals") = ADS_CHASE_REFERRALS_SUBORDINATE

    objCommand.CommandText = _
    "SELECT distinguishedName FROM 'GC://DC=DOMAIN,DC=COM' WHERE " _
    & "objectClass = 'group' AND Name = '" & group & "'"
    Set objRecordSet = objCommand.Execute
    objRecordSet.MoveFirst
    If err.Number <> 0 Then
    objError = MsgBox("Could not find a group by that name. Please try again or cancel.",vbOKOnly,"No Such Group")
    WScript.Echo "Could not find a group by that name."
    err.Number = 0
    strGroup = ""
    strGroupName = ""
    GetGroup
    Else
    Set objGroup = GetObject("LDAP://" & objRecordSet.Fields("distinguishedName").Value)
    Output(objGroup)
    End If
    End Function


    ------------------------------------------------
    The above is a VBS script. Below is a BAT file to run the script.
    ------------------------------------------------
    @cscript.exe <scriptname>.vbs //nologo

    +
    0 Votes

    The OP gave up years ago, or has since found another line of work. Zombies should not be awakened.

  • +
    0 Votes
    robo_dev

    so you need to do a LDAP query using VB

    http://techtasks.com/code/viewbookcode/1525

    +
    0 Votes
    Rabbit_Runner

    'Script created on 5 December 2007
    'Prompts user to enter friendly name of a group and then dumps
    'the display names of the members of that group to both screen and
    'a text file on the root of Where the vbs file resides.

    '******************************DECLARATIONS**********************************
    Const ADS_SCOPE_SUBTREE = 2
    Const ADS_CHASE_REFERRALS_SUBORDINATE = &H20
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set DataList = CreateObject("System.Collections.ArrayList")

    dim sCurPath
    sCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".") & "\"
    'WScript.Echo sCurPath


    strGroupName = GetGroup
    'WScript.Echo GetGroup


    '******************************FUNCTIONS**********************************
    Function Output(group)
    group.GetInfo
    arrMemberOf = group.GetEx("member")
    For Each strMember in arrMemberOf
    strLDAPPath = "LDAP://" & strMember
    Set objUser = GetObject(strLDAPPath)
    DataList.Add objUser.DisplayName
    Next
    DataList.Sort()
    'strOutputPath = "c:\" & group.DisplayName & ".txt"
    strOutputPath = sCurPath & right(group.name,(len(group.name)-3)) & ".txt"
    wscript.echo strOutputPath
    Set objOutput = objFSO.CreateTextFile(strOutputPath)
    WScript.Echo "Members of " & group.name
    For Each strItem in DataList
    intTotalMembers = intTotalMembers + 1
    objOutput.WriteLine strItem
    WScript.Echo strItem
    Next
    WScript.Echo
    WScript.Echo intTotalMembers & " total members of " & group.Name & "."
    objFinal = MsgBox("File has been saved as " & strOutputPath,vbOKOnly,"File Information")
    End Function

    Function GetGroup
    strGroup = InputBox("Enter the group name.","Enter Name")
    If strGroup = "" Then
    WScript.Quit
    Else
    Main(strGroup)
    End If
    End Function

    Function Main(group)
    On Error Resume 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("Chase referrals") = ADS_CHASE_REFERRALS_SUBORDINATE

    objCommand.CommandText = _
    "SELECT distinguishedName FROM 'GC://DC=DOMAIN,DC=COM' WHERE " _
    & "objectClass = 'group' AND Name = '" & group & "'"
    Set objRecordSet = objCommand.Execute
    objRecordSet.MoveFirst
    If err.Number <> 0 Then
    objError = MsgBox("Could not find a group by that name. Please try again or cancel.",vbOKOnly,"No Such Group")
    WScript.Echo "Could not find a group by that name."
    err.Number = 0
    strGroup = ""
    strGroupName = ""
    GetGroup
    Else
    Set objGroup = GetObject("LDAP://" & objRecordSet.Fields("distinguishedName").Value)
    Output(objGroup)
    End If
    End Function


    ------------------------------------------------
    The above is a VBS script. Below is a BAT file to run the script.
    ------------------------------------------------
    @cscript.exe <scriptname>.vbs //nologo

    +
    0 Votes

    The OP gave up years ago, or has since found another line of work. Zombies should not be awakened.