Active directory group membership check

gekko66

New Member
Joined
Mar 14, 2012
Messages
12
Hi, I am trying to produce a macro that will allow me to run different scripts based on user group membership however it fails to recognise the AD groups.any help is appreciated.
'******************************************************************************
'** Begin of modification block
'** Modify Constants and variables:
Sub Checker()
Dim checkedGroups()
Dim WshNetwork, strUser, strGroup
Dim SiteDepartments()
ReDim SiteDepartments(4, 0)
Const SITE = "UnitedKingdom"

On Error Resume Next
' Set WshNetwork = WScript.CreateObject("WScript.Network")
strUser = WshNetwork.UserName
If ifmember(strUser, "gg_UnitedKingdom_Financial") = 1 Then
Else
If ifmember(strUser, "gg_UnitedKingdom_Marketing") = 1 Then
Else
If ifmember(strUser, "gg_UnitedKingdom_Group_Sales") = 1 Then
Else
If ifmember(strUser, "gg_UnitedKingdom_Group_Supplychain") = 1 Then
Else
If ifmember(strUser, "gg_UnitedKingdom_IT") = 1 Then
End If
End If
End If
End If
End If
End If
End Sub
'*******************************
'** FUNCTIONS AND SUBROUTINES **
'*******************************
'* ifmember(usr, grp)
'* DESCRIPTION: This function will check if a user is in the group or in a nested group
'* PARAMETERS: String usr.......the username to be tested
'* String grp.......the parent group to seek the user.
'* the paramgrp() array is used by the function itself
'*
'* RETURNVALUE: Integer 0........User was NOT found in the (nested) group
'* Integer 1........User was found in the group
'****************************************************************************************
Function ifmember(paramusr, paramgrp())
Dim usr, grp, arrNestedGroups(), oGroup, bSkip, curGroup
Dim oGC, oContainer, oConnection, oRecordset
usr = paramusr
grp = paramgrp
On Error Resume Next
ReDim arrNestedGroups(0)
If IsArray(grp) = False Then
ReDim Preserve arrNestedGroups(1)
ReDim checkedGroups(0)
'*****************************************
'Get the Distinguished name of the group
Set oContainer = GetObject("GC:")
For Each oGC In oContainer
ForestPath = oGC.adsPath
Next
ForestQuery = "<" & ForestPath & ">;(&(objectClass=Group)(sAMAccountName=" & grp & "));distinguishedName;subtree"
Set oConnection = CreateObject("ADODB.Connection")
Set oRecordset = CreateObject("ADODB.Recordset")
oConnection.Provider = "ADSDSOObject"
oConnection.Open
Set oRecordset = oConnection.Execute(ForestQuery)
If oRecordset.EOF And oRecordset.BOF Then
ifmember = 0
Exit Function
Else
While Not oRecordset.EOF
grp = "LDAP://" & oRecordset.Fields("distinguishedName")
oRecordset.MoveNext
Wend
End If
'*****************************************
'Translate the user to his Distinguished name
ForestQuery = "<" & ForestPath & ">;(&(objectClass=User)(sAMAccountName=" & usr & "));distinguishedName;subtree"
Set oRecordset = oConnection.Execute(ForestQuery)
If oRecordset.EOF And oRecordset.BOF Then
ifmember = 0
Exit Function
Else
While Not oRecordset.EOF
usr = "LDAP://" & oRecordset.Fields("distinguishedName")
oRecordset.MoveNext
Wend
End If
Set oGC = Nothing
Set oContainer = Nothing
Set oConnection = Nothing
Set oRecordset = Nothing
arrNestedGroups(0) = grp
grp = arrNestedGroups
ReDim arrNestedGroups(0)
End If
For i = 0 To UBound(grp) - 1
curGroup = grp(i)
bSkip = False
For Z = 0 To UBound(checkedGroups) - 1
tmp = checkedGroups(Z)
If tmp = curGroup Then
bSkip = True
Exit For
End If
Next
If bSkip = False Then
Max = UBound(checkedGroups)
ReDim Preserve checkedGroups(Max + 1)
checkedGroups(Max) = curGroup
Err.Clear
Set oGroup = GetObject(curGroup)
If Err.Number = 0 Then
For Each oUser In oGroup.Members
'wscript.echo "Parsing " & oUser.adsPath & Chr(13) & "Class: " & oUser.Class
If LCase(oUser.Class) = "group" Then
Max = UBound(arrNestedGroups)
ReDim Preserve arrNestedGroups(Max + 1)
arrNestedGroups(Max) = oUser.adsPath
Else
'wscript.echo "User " & oUser.Name & ", usr: " & usr
If oUser.adsPath = usr Then
ifmember = 1
Exit Function
End If
End If
Next
End If
Set oGroup = Nothing
End If
Next
If UBound(arrNestedGroups) > 0 Then
nResult = ifmember(usr, arrNestedGroups)
ifmember = nResult
Exit Function
End If
ifmember = 0
End Function
'****************************************************************************************
'* showerror(msg)
'* DESCRIPTION: This Subroutine will show an custom error message along with Err properties
'* PARAMETERS: String msg.......Your custom message
'*
'* Sample: showError "Oops, an error occured..."
'*
'****************************************************************************************
Sub showerror(msg)
WScript.echo "Logon Script (" & WScript.ScriptFullName & ")" & Chr(13) & "errno: " & Err.Number & Chr(13) & "errdescr: " & Err.Description & Chr(13) & msg
Err.Clear
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top