Question:
I am writing a script that queries Active Directory for information as
such:
set oGrp = GetObject(path)
mems = oGrp.GetEx("member")
I'm writing most of this script off-site, so I don't have access to AD.
I'd like to create a dummy collection like that which is returned by
oGrp.GetEx( "member" ) so I can test the rest of my code.
Is Dictionary my best option? Does anybody have any other
suggestions?
Answer:
-Download a free window server trial, free vmware.com virtual server,
load the os and create a local domain to run all the scripts in that
sandbox.
-The member attribute of group objects (and the memberOf attribute of user,
computer, and group objects) is multi-valued. Any code to handle these
attributes must handle the three possible situations: no values, one value,
or more than one value in the collection. Also, "Primary" group membership
is never revealed by these attributes.
Using the GetEx method to retrieve member (or memberOf) will raise an error
if there are no values in the collection. If there is one value or more, the
variable "mems" in your example will have datatype "Variant()", which can be
enumerated in a "For Each" loop.
If you use the GetEx method, you can trap the possible error with code
similar to below:
==============
strPath = "cn=TestGroup,ou=Sales,dc=MyDomain,dc=com")
Set objGroup = GetObject(strPath)
On Error Resume Next
arrMembers = objGroup.GetEx("member")
If (Err.Number <> 0) Then
On Error GoTo 0
Wscript.Echo "The member attribute is empty"
Else
On Error GoTo 0
For Each strMember In arrMembers
Wscript.Echo strMember
Next
End If
============
If you use the Get method instead of GetEx, the Get method will still raise
an error if the member attribute has no values. However, if there is one
value, arrMembers (in my example code) will be datatype "String", and if
there is more than one value it will be datatype "Variant()". You could use
code similar to:
==============
strPath = "cn=TestGroup,ou=Sales,dc=MyDomain,dc=com")
Set objGroup = GetObject(strPath)
On Error Resume Next
arrMembers = objGroup.Get("member")
If (Err.Number <> 0) Then
On Error GoTo 0
Wscript.Echo "The member attribute is empty"
ElseIf (TypeName(arrMembers) = "String") Then
Wscript.Echo arrMembers
Else
On Error GoTo 0
For Each strMember In arrMembers
Wscript.Echo strMember
Next
End If
============
I prefer to retrieve the member attribute directly, without using either the
GetEx or Get method, in which case the variable arrMembers (in my example
code) is "Empty" if there are no values, a "String" if there is one value,
and datatype "Variant()" if there is more than one value in the collection.
No error trapping is needed. For example:
=============
strPath = "cn=TestGroup,ou=Sales,dc=MyDomain,dc=com")
Set objGroup = GetObject(strPath)
arrMembers = objGroup.member
If IsEmpty(arrMembers) Then
Wscript.Echo "The member attribute is empty"
ElseIf (TypeName(arrMembers) = "String") Then
Wscript.Echo arrMembers
Else
For Each strMember In arrMembers
Wscript.Echo strMember
Next
End If
============
Another method would be to use the Members method of the group object, which
returns a collection of member objects. For example:
=============
strPath = "cn=TestGroup,ou=Sales,dc=MyDomain,dc=com")
Set objGroup = GetObject(strPath)
For Each objMember in objGroup.Members
Wscript.Echo objMember.distinguishedName
Next
=============
This might be a bit slower, since the program must bind to each member
object (although I doubt you could notice), but it has the advantage that
with the object reference, you can refer to any other attributes of the
member objects, such as sAMAccountName (the "pre-Windows 2000 logon name" or
NetBIOS name).
I don't see how to create an array that mimics the behaviour of the member
attribute in all cases. However, in most cases (when there are at least 2
members of the group) it is datatype "Variant()", which can be simulated by
using the Array function.