WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Samples from Users

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

ADSI COM Find Primary Group


DomainName = "Flash"
UserLoginName = "gordon"

;
; Bind to the user object with the Windows NT provider.
;
oUsr = GetObject("WinNT://" : DomainName : "/" : UserLoginName : ",user")
grp = oUsr.Groups
GrpID = oUsr.PrimaryGroupID
GrpName = ""

;
; Building Query Filter for the search for all the groups that the user is a member of.
;
QueryFilter = "(|"
ForEach Item In Grp
   NT4Name = StrReplace(Item.ADsPath,"WinNT://","")
   tempArray = Arrayize(nt4Name,"/")
   NT4Name = tempArray[1]
   QueryFilter = QueryFilter : "(samAccountName=" : NT4Name : ")"
Next
QueryFilter = QueryFilter : ")"

;
; Building LDAP dialect Query String.
;
QueryString = "<LDAP://" : DomainName : ">;" : QueryFilter : ";PrimaryGroupToken,distinguishedName;subtree"

;
; Performing Query against the Active Directory for all the groups that
; the user belongs to and retrieving the RID of the group object off
; the PrimaryGroupToken attribute on the user.
;
oConnection = CreateObject("ADODB.Connection")
oCommand = CreateObject("ADODB.Command")

oConnection.Provider = "ADsDSOObject"
oConnection.Open( "Active Directory Provider" )

oCommand.ActiveConnection = oConnection
oCommand.CommandText = QueryString
oCommand.Properties("Page Size") = 900

oRecordset = oCommand.Execute

;
; Looping through all the records in the search result to determine whether
; any of these group's PrimaryGroupToken attribute value match the
; PrimaryGroupID attribute value stored on the user object.
;
bGroupFound = 0
While ((! oRecordset.EOF) && (! bGroupFound))
  If (GrpID == oRecordset.Fields("PrimaryGroupToken").value) Then
    GrpName = oRecordset.Fields("DistinguishedName").Value
    bGroupFound = 1
  End If
  oRecordset.moveNext
EndWhile

oRecordset = 0
oCommand = 0
oConnection = 0

;
; Displaying Results of the search.
;
If( bGroupFound ) Then
   Pause( "Primary Group for " , oUsr.AdsPath : @LF :"Is: " : GrpName)
Else
   Pause( "Notice", "Primary Group Not Found" )
End If

Article ID:   W17367
File Created: 2010:04:16:10:37:10
Last Updated: 2010:04:16:10:37:10