List all the members of a group error when there are no members<p>Language:  VB Script<br/>Reference:  Hey Scripting Guy!  <a href="http://technet.microsoft.com/scriptcenter/default.aspx">Script Center Home</a> &gt; <a href="http://social.technet.microsoft.com/technet/scriptcenter/scripts/default.mspx">Scripts</a> &gt; <a href="http://social.technet.microsoft.com/technet/scriptcenter/scripts/ad/default.mspx">Active Directory</a> &gt; <a href="http://social.technet.microsoft.com/technet/scriptcenter/scripts/ad/groups/default.mspx">Groups</a> &gt; List All the Members of a Group<br/><br/>It has a problem when there are no members of the group!  If there are no members of the group, it provides an erroneous result for that group.  I see two choices... 1.  pull up the membership property of each member and cross verify or 2. Somehow check the member property of the group to see if it's null.  I would prefer the second option if someone knows how to code it.  Here's my script so far...<br/><br/>' *****************************<br/>' * List All Groups in the Domain and<br/>' * List All Members of each Group<br/>' * <br/>' * Output to a text file on the user's desktop in the format:<br/>' * group name &lt;tab&gt; type &lt;tab&gt; member name &lt;tab&gt; type<br/>' * Prompt for text file name.<br/>' *****************************<br/>Const MY_DOMAIN = &quot;dc=contoso,dc=com&quot;<br/>' *****************************<br/>' Start Main<br/>On Error Resume Next<br/>Const ADS_SCOPE_SUBTREE = 2<br/>Const ADS_GROUP_TYPE_GLOBAL_GROUP = &amp;h2<br/>Const ADS_GROUP_TYPE_LOCAL_GROUP = &amp;h4<br/>Const ADS_GROUP_TYPE_UNIVERSAL_GROUP = &amp;h8<br/>Const ADS_GROUP_TYPE_SECURITY_ENABLED = &amp;h80000000<br/>Const ForReading = 1, ForWriting = 2, ForAppending = 8<br/>Const myPrompt = &quot;Enter the Output filename (i.e. Groups.txt) that will be saved on your desktop:&quot;<br/>'Get filename to create and set it up to receive input<br/>If UCase( Right( WScript.FullName, 12 ) ) = &quot;\CSCRIPT.EXE&quot; Then<br/>  WScript.StdOut.Write myPrompt &amp; &quot; &quot;<br/>  strMyFileName = WScript.StdIn.ReadLine<br/>Else<br/>  strMyFileName = InputBox( myPrompt )<br/>End If<br/>if strMyFileName = &quot;&quot; then<br/>  wscript.quit<br/>end if<br/>Set WshShell = CreateObject(&quot;WScript.Shell&quot;)<br/>Set WshSysEnv = WshShell.Environment(&quot;PROCESS&quot;)<br/>strMyFileName = WshSysEnv(&quot;USERPROFILE&quot;) &amp; &quot;\Desktop\&quot; &amp; strMyFileName<br/>Set WshSysEnv = nothing<br/>Set WshShell = nothing<br/>Set objFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)<br/>if objFSO.FileExists(strMyFileName) then<br/>  wscript.echo &quot;That filename already exists&quot;<br/>  wscript.quit<br/>end if<br/>Set objMyOutput = objFSO.OpenTextFile(strMyFileName, ForWriting, True)<br/>' Enumerate the groups in the domain<br/>Set objConnection = CreateObject(&quot;ADODB.Connection&quot;)<br/>Set objCommand =   CreateObject(&quot;ADODB.Command&quot;)<br/>objConnection.Provider = &quot;ADsDSOObject&quot;<br/>objConnection.Open &quot;Active Directory Provider&quot;<br/>Set objCommand.ActiveConnection = objConnection<br/>objCommand.Properties(&quot;Page Size&quot;) = 1000<br/>objCommand.Properties(&quot;Searchscope&quot;) = ADS_SCOPE_SUBTREE <br/>objCommand.CommandText = _<br/>    &quot;SELECT ADsPath, Name FROM 'LDAP://&quot; &amp; MY_DOMAIN &amp; &quot;' WHERE objectCategory='group'&quot;</p> <p>Set objRecordSet = objCommand.Execute</p> <p>objRecordSet.MoveFirst</p> <p>Do Until objRecordSet.EOF<br/>  Set objGroup = GetObject(objRecordSet.Fields(&quot;ADsPath&quot;).Value)<br/>  strGroupName = objRecordSet.Fields(&quot;Name&quot;).Value<br/>  If objGroup.GroupType AND ADS_GROUP_TYPE_LOCAL_GROUP Then<br/>    strGroupDesc = &quot;Domain local &quot;<br/>  ElseIf objGroup.GroupType AND ADS_GROUP_TYPE_GLOBAL_GROUP Then<br/>    strGroupDesc = &quot;Global &quot;<br/>  ElseIf objGroup.GroupType AND ADS_GROUP_TYPE_UNIVERSAL_GROUP Then<br/>    strGroupDesc = &quot;Universal &quot;<br/>  Else<br/>    strGroupDesc = &quot;Unknown &quot;<br/>  End If<br/>  If objGroup.GroupType AND ADS_GROUP_TYPE_SECURITY_ENABLED Then<br/>    strGroupDesc = strGroupDesc &amp; &quot;Security group&quot;<br/>  Else<br/>    strGroupDesc = strGroupDesc &amp; &quot;Distribution group&quot;<br/>  End If<br/>  <br/>  ' Reference List all the members of a group<br/>  For Each strMemberOf in objGroup.Member<br/>    Set objMember = GetObject(&quot;LDAP://&quot; &amp; strMemberOf)<br/>    strMemberName = right(objMember.Name,len(objMember.Name)-3)<br/>    ' wscript.echo strGroupName &amp; vbcrlf &amp; strGroupDesc &amp; vbcrlf &amp; strMemberName &amp; vbcrlf &amp; objMember.Class<br/>    objMyOutput.WriteLine(strGroupName &amp; vbtab &amp; strGroupDesc &amp; vbtab &amp; strMemberName &amp; vbtab &amp; objMember.Class)<br/>  Next<br/>  objRecordSet.MoveNext<br/>Loop<br/>objMyOutput.close<br/></p>© 2009 Microsoft Corporation. All rights reserved.Thu, 02 Jul 2009 15:08:33 Z4c50b737-0329-4bf4-b57b-2a5a7696b150http://social.technet.microsoft.com/Forums/en/ITCG/thread/4c50b737-0329-4bf4-b57b-2a5a7696b150#4c50b737-0329-4bf4-b57b-2a5a7696b150http://social.technet.microsoft.com/Forums/en/ITCG/thread/4c50b737-0329-4bf4-b57b-2a5a7696b150#4c50b737-0329-4bf4-b57b-2a5a7696b150James Andersonhttp://social.technet.microsoft.com/Profile/en-US/?user=James%20AndersonList all the members of a group error when there are no members<p>Language:  VB Script<br/>Reference:  Hey Scripting Guy!  <a href="http://technet.microsoft.com/scriptcenter/default.aspx">Script Center Home</a> &gt; <a href="http://social.technet.microsoft.com/technet/scriptcenter/scripts/default.mspx">Scripts</a> &gt; <a href="http://social.technet.microsoft.com/technet/scriptcenter/scripts/ad/default.mspx">Active Directory</a> &gt; <a href="http://social.technet.microsoft.com/technet/scriptcenter/scripts/ad/groups/default.mspx">Groups</a> &gt; List All the Members of a Group<br/><br/>It has a problem when there are no members of the group!  If there are no members of the group, it provides an erroneous result for that group.  I see two choices... 1.  pull up the membership property of each member and cross verify or 2. Somehow check the member property of the group to see if it's null.  I would prefer the second option if someone knows how to code it.  Here's my script so far...<br/><br/>' *****************************<br/>' * List All Groups in the Domain and<br/>' * List All Members of each Group<br/>' * <br/>' * Output to a text file on the user's desktop in the format:<br/>' * group name &lt;tab&gt; type &lt;tab&gt; member name &lt;tab&gt; type<br/>' * Prompt for text file name.<br/>' *****************************<br/>Const MY_DOMAIN = &quot;dc=contoso,dc=com&quot;<br/>' *****************************<br/>' Start Main<br/>On Error Resume Next<br/>Const ADS_SCOPE_SUBTREE = 2<br/>Const ADS_GROUP_TYPE_GLOBAL_GROUP = &amp;h2<br/>Const ADS_GROUP_TYPE_LOCAL_GROUP = &amp;h4<br/>Const ADS_GROUP_TYPE_UNIVERSAL_GROUP = &amp;h8<br/>Const ADS_GROUP_TYPE_SECURITY_ENABLED = &amp;h80000000<br/>Const ForReading = 1, ForWriting = 2, ForAppending = 8<br/>Const myPrompt = &quot;Enter the Output filename (i.e. Groups.txt) that will be saved on your desktop:&quot;<br/>'Get filename to create and set it up to receive input<br/>If UCase( Right( WScript.FullName, 12 ) ) = &quot;\CSCRIPT.EXE&quot; Then<br/>  WScript.StdOut.Write myPrompt &amp; &quot; &quot;<br/>  strMyFileName = WScript.StdIn.ReadLine<br/>Else<br/>  strMyFileName = InputBox( myPrompt )<br/>End If<br/>if strMyFileName = &quot;&quot; then<br/>  wscript.quit<br/>end if<br/>Set WshShell = CreateObject(&quot;WScript.Shell&quot;)<br/>Set WshSysEnv = WshShell.Environment(&quot;PROCESS&quot;)<br/>strMyFileName = WshSysEnv(&quot;USERPROFILE&quot;) &amp; &quot;\Desktop\&quot; &amp; strMyFileName<br/>Set WshSysEnv = nothing<br/>Set WshShell = nothing<br/>Set objFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)<br/>if objFSO.FileExists(strMyFileName) then<br/>  wscript.echo &quot;That filename already exists&quot;<br/>  wscript.quit<br/>end if<br/>Set objMyOutput = objFSO.OpenTextFile(strMyFileName, ForWriting, True)<br/>' Enumerate the groups in the domain<br/>Set objConnection = CreateObject(&quot;ADODB.Connection&quot;)<br/>Set objCommand =   CreateObject(&quot;ADODB.Command&quot;)<br/>objConnection.Provider = &quot;ADsDSOObject&quot;<br/>objConnection.Open &quot;Active Directory Provider&quot;<br/>Set objCommand.ActiveConnection = objConnection<br/>objCommand.Properties(&quot;Page Size&quot;) = 1000<br/>objCommand.Properties(&quot;Searchscope&quot;) = ADS_SCOPE_SUBTREE <br/>objCommand.CommandText = _<br/>    &quot;SELECT ADsPath, Name FROM 'LDAP://&quot; &amp; MY_DOMAIN &amp; &quot;' WHERE objectCategory='group'&quot;</p> <p>Set objRecordSet = objCommand.Execute</p> <p>objRecordSet.MoveFirst</p> <p>Do Until objRecordSet.EOF<br/>  Set objGroup = GetObject(objRecordSet.Fields(&quot;ADsPath&quot;).Value)<br/>  strGroupName = objRecordSet.Fields(&quot;Name&quot;).Value<br/>  If objGroup.GroupType AND ADS_GROUP_TYPE_LOCAL_GROUP Then<br/>    strGroupDesc = &quot;Domain local &quot;<br/>  ElseIf objGroup.GroupType AND ADS_GROUP_TYPE_GLOBAL_GROUP Then<br/>    strGroupDesc = &quot;Global &quot;<br/>  ElseIf objGroup.GroupType AND ADS_GROUP_TYPE_UNIVERSAL_GROUP Then<br/>    strGroupDesc = &quot;Universal &quot;<br/>  Else<br/>    strGroupDesc = &quot;Unknown &quot;<br/>  End If<br/>  If objGroup.GroupType AND ADS_GROUP_TYPE_SECURITY_ENABLED Then<br/>    strGroupDesc = strGroupDesc &amp; &quot;Security group&quot;<br/>  Else<br/>    strGroupDesc = strGroupDesc &amp; &quot;Distribution group&quot;<br/>  End If<br/>  <br/>  ' Reference List all the members of a group<br/>  For Each strMemberOf in objGroup.Member<br/>    Set objMember = GetObject(&quot;LDAP://&quot; &amp; strMemberOf)<br/>    strMemberName = right(objMember.Name,len(objMember.Name)-3)<br/>    ' wscript.echo strGroupName &amp; vbcrlf &amp; strGroupDesc &amp; vbcrlf &amp; strMemberName &amp; vbcrlf &amp; objMember.Class<br/>    objMyOutput.WriteLine(strGroupName &amp; vbtab &amp; strGroupDesc &amp; vbtab &amp; strMemberName &amp; vbtab &amp; objMember.Class)<br/>  Next<br/>  objRecordSet.MoveNext<br/>Loop<br/>objMyOutput.close<br/></p>Wed, 01 Jul 2009 21:03:45 Z2009-07-01T21:03:45Zhttp://social.technet.microsoft.com/Forums/en/ITCG/thread/4c50b737-0329-4bf4-b57b-2a5a7696b150#fb053105-572f-41c0-b286-8053a380f7e2http://social.technet.microsoft.com/Forums/en/ITCG/thread/4c50b737-0329-4bf4-b57b-2a5a7696b150#fb053105-572f-41c0-b286-8053a380f7e2Salvador Manaois IIIhttp://social.technet.microsoft.com/Profile/en-US/?user=Salvador%20Manaois%20IIIList all the members of a group error when there are no membersI haven't tested your script yet but I see that you are enumerating members of the objGroup.Member object (instead of objGroup.Members). Is this a typo?<br/><br/>An alternative way of getting the members of all groups in your AD is to use a combination of DSQUERY and DSGET, as shown:<br/><br/><strong>dsquery group | dsget group -members<br/><br/></strong>This command will list all groups in your AD through DSQUEY, pipes each group to DSGET to list the members of the group. Empty groups will have, well, empty entries. =)<br/><br/>Regards,<br/><br/><strong>Salvador Manaois III<br/></strong>MCITP | <em>Enterprise &amp; Server Administrator<br/></em>MCSE MCSA MCTS(x5) CIWA C|EH <br/>My Blog: <a href="http://badzmanaois.blogspot.com/"><span style="color:#0033cc"><strong>Bytes and Badz</strong></span></a> <br/>My Shots: <a href="http://social.technet.microsoft.com/Forums/en-US/user/flickr.com/photos/badzmanaois"><span style="color:#0033cc"><strong>View My PhotoStream</strong></span></a>Thu, 02 Jul 2009 04:47:51 Z2009-07-02T04:47:51Zhttp://social.technet.microsoft.com/Forums/en/ITCG/thread/4c50b737-0329-4bf4-b57b-2a5a7696b150#2a16e7b4-218b-4507-a788-ff4a9d713993http://social.technet.microsoft.com/Forums/en/ITCG/thread/4c50b737-0329-4bf4-b57b-2a5a7696b150#2a16e7b4-218b-4507-a788-ff4a9d713993James Andersonhttp://social.technet.microsoft.com/Profile/en-US/?user=James%20AndersonList all the members of a group error when there are no members<p>I have when you powershell guys do in one line what I did in an entire script, but mine still adds the type descriptions.  I found out the hard way that set object = nothing does have its uses along with some error checking!  Note to self... when looping a piece of vb script and setting an object in the loop, make sure and clear objects at the end of the loop before they get reused.  It made a difference.  I also added error checking of Err.Number to make sure the object.parameter existed and that cleaned up the output as well.  I checked it against Active Directory and it cleared up the erroneous entries.  The key was checking the error against E_ADS_PROPERTY_NOT_FOUND. Here's the result...<br/><br/>' *****************************<br/>' * List All Groups in the Domain and<br/>' * List All Members of each Group<br/>' * <br/>' * Output to a text file on the user's desktop in the format:<br/>' * group name &lt;tab&gt; type &lt;tab&gt; member name &lt;tab&gt; type<br/>' * Prompt for text file name.<br/>' *****************************<br/>' Variables<br/>Const MY_DOMAIN = &quot;dc=contoso,dc=com&quot;<br/>' *****************************<br/>' Start Main<br/>On Error Resume Next<br/>Const ADS_SCOPE_SUBTREE = 2<br/>Const ADS_GROUP_TYPE_GLOBAL_GROUP = &amp;h2<br/>Const ADS_GROUP_TYPE_LOCAL_GROUP = &amp;h4<br/>Const ADS_GROUP_TYPE_UNIVERSAL_GROUP = &amp;h8<br/>Const ADS_GROUP_TYPE_SECURITY_ENABLED = &amp;h80000000<br/>Const E_ADS_PROPERTY_NOT_FOUND = &amp;h8000500D<br/>Const MYPROMPT = &quot;Enter the Output filename (i.e. Groups.txt) that will be saved on your desktop:&quot;<br/>Const ForReading = 1, ForWriting = 2, ForAppending = 8<br/>Set objFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)</p> <p>' Setup the output file<br/>If UCase( Right( WScript.FullName, 12 ) ) = &quot;\CSCRIPT.EXE&quot; Then<br/>  WScript.StdOut.Write MYPROMPT &amp; &quot; &quot;<br/>  strMyFileName = WScript.StdIn.ReadLine<br/>Else<br/>  strMyFileName = InputBox( MYPROMPT )<br/>End If<br/>if strMyFileName = &quot;&quot; then<br/>  wscript.quit<br/>end if<br/>Set WshShell = CreateObject(&quot;WScript.Shell&quot;)<br/>Set WshSysEnv = WshShell.Environment(&quot;PROCESS&quot;)<br/>strMyFileName = WshSysEnv(&quot;USERPROFILE&quot;) &amp; &quot;\Desktop\&quot; &amp; strMyFileName<br/>Set WshSysEnv = nothing<br/>Set WshShell = nothing<br/>if objFSO.FileExists(strMyFileName) then<br/>  'objFSO.DeleteFile(strMyFileName)<br/>  wscript.echo &quot;That filename already exists&quot;<br/>  wscript.quit<br/>end if</p> <p>' Get a recordset of groups in AD<br/>Set objMyOutput = objFSO.OpenTextFile(strMyFileName, ForWriting, True)<br/>Set objConnection = CreateObject(&quot;ADODB.Connection&quot;)<br/>Set objCommand =   CreateObject(&quot;ADODB.Command&quot;)<br/>objConnection.Provider = &quot;ADsDSOObject&quot;<br/>objConnection.Open &quot;Active Directory Provider&quot;<br/>Set objCommand.ActiveConnection = objConnection<br/>objCommand.Properties(&quot;Page Size&quot;) = 1000<br/>objCommand.Properties(&quot;Searchscope&quot;) = ADS_SCOPE_SUBTREE <br/>objCommand.CommandText = _<br/>    &quot;SELECT ADsPath, Name FROM 'LDAP://&quot; &amp; MY_DOMAIN &amp; &quot;' WHERE objectCategory='group'&quot; <br/>Set objRecordSet = objCommand.Execute<br/>objRecordSet.MoveFirst</p> <p>' For each Group, Get group properties<br/>Do Until objRecordSet.EOF<br/>  Set objGroup = GetObject(objRecordSet.Fields(&quot;ADsPath&quot;).Value)<br/>  strGroupName = objRecordSet.Fields(&quot;Name&quot;).Value<br/>  If objGroup.GroupType AND ADS_GROUP_TYPE_LOCAL_GROUP Then<br/>    strGroupDesc = &quot;Domain local &quot;<br/>  ElseIf objGroup.GroupType AND ADS_GROUP_TYPE_GLOBAL_GROUP Then<br/>    strGroupDesc = &quot;Global &quot;<br/>  ElseIf objGroup.GroupType AND ADS_GROUP_TYPE_UNIVERSAL_GROUP Then<br/>    strGroupDesc = &quot;Universal &quot;<br/>  Else<br/>    strGroupDesc = &quot;Unknown &quot;<br/>  End If<br/>  If objGroup.GroupType AND ADS_GROUP_TYPE_SECURITY_ENABLED Then<br/>    strGroupDesc = strGroupDesc &amp; &quot;Security group&quot;<br/>  Else<br/>    strGroupDesc = strGroupDesc &amp; &quot;Distribution group&quot;<br/>  End If</p> <p>  ' Check if there are members<br/>  err.clear<br/>  arrMemberOf = objGroup.GetEx(&quot;Member&quot;)<br/>  If Err.Number = E_ADS_PROPERTY_NOT_FOUND then<br/>    ' Write a line to the outputfile with group properties and no members<br/>    objMyOutput.WriteLine(strGroupName &amp; vbtab &amp; strGroupDesc &amp; vbtab &amp; &quot;&lt;null&gt;&quot; &amp; vbtab &amp; &quot;&lt;null&gt;&quot;)<br/>  Else<br/>    ' For each group member, get member properties<br/>    For Each strMemberOf in arrMemberOf<br/>      Set objMember = GetObject(&quot;LDAP://&quot; &amp; strMemberOf)<br/>      strMemberName = right(objMember.Name,len(objMember.Name)-3)<br/>      ' Write a line to the outputfile with group and member properties<br/>      objMyOutput.WriteLine(strGroupName &amp; vbtab &amp; strGroupDesc &amp; vbtab &amp; strMemberName &amp; vbtab &amp; objMember.Class)<br/>      set objMember = nothing<br/>    Next<br/>  End If<br/>  objRecordSet.MoveNext<br/>  Set objGroup = nothing<br/>Loop<br/>objMyOutput.close</p>Thu, 02 Jul 2009 14:58:05 Z2009-07-02T14:58:05Zhttp://social.technet.microsoft.com/Forums/en/ITCG/thread/4c50b737-0329-4bf4-b57b-2a5a7696b150#2d10c48a-81fc-4e2b-a52b-29e716c1a5c8http://social.technet.microsoft.com/Forums/en/ITCG/thread/4c50b737-0329-4bf4-b57b-2a5a7696b150#2d10c48a-81fc-4e2b-a52b-29e716c1a5c8James Andersonhttp://social.technet.microsoft.com/Profile/en-US/?user=James%20AndersonList all the members of a group error when there are no membersAs to your question on objGroup.Member, ldp.exe has the property listed as &quot;member&quot;, not &quot;members&quot;.  It works with objGroup.Member (wasn't a typo).  I have found ldp.exe a great tool for looking up property names and seeing what the property values are.  The schema management console also does a nice job of listing properties.  I recommend both to anyone writing  AD scripts.Thu, 02 Jul 2009 15:08:33 Z2009-07-02T15:08:33Z