Script repository
The script allows you to execute custom commands when membership of an AD group changes, no matter whether the changes were made using Adaxes or any third party tools, such as ADUC or Exchange. To be able to identify members added to or removed from a group, the script saves GUIDs of the current members in a multi-valued attribute of the group. To execute the script, create a scheduled task configured for the Group object type and add the required groups to the Activity Scope of the task.
Parameters
$savedMembersAttribute- Specifies the LDAP display name of the attribute that will be used to store group member GUIDs.$customCommandIdAddedToGroup- the identifier of the custom commands that will be executed on users added to a group. For details on how to obtain the identifier, see Get custom command identifier.$customCommandIdRemovedFromGroup- the identifier of the custom commands that will be executed on users removed from a group.
$savedMembersAttribute = "adm-CustomAttributeTextMultiValue1" # TODO: modify me
$customCommandIdAddedToGroup = "{D993945F-A9B7-4349-AE18-351F80D9DCD9}" # TODO: modify me
$customCommandIdRemovedFromGroup = "{9DB88EC3-1241-4AB1-9612-C7C982BAA49F}" # TODO: modify me
function SaveCurrentMembers($guidsBytes, $savedMembersAttribute)
{
if ($guidsBytes.Count -eq 0)
{
# All members were removed from the group.
$Context.TargetObject.PutEx("ADS_PROPERTY_UPDATE", $savedMembersAttribute, @("none"))
}
else
{
$values = @()
foreach ($guidBytes in $guidsBytes)
{
$values += ([Guid]$guidBytes).ToString()
}
$Context.TargetObject.PutEx("ADS_PROPERTY_UPDATE", $savedMembersAttribute, $values)
}
# Save changes
$Context.TargetObject.SetInfo()
}
# Get GUIDs of direct members of the group.
try
{
$currentMemberGuids = $Context.TargetObject.GetEx("adm-DirectMembersGuid")
}
catch
{
$currentMemberGuids = @()
}
$memberGuids = New-Object "System.Collections.Generic.HashSet[System.Guid]"
foreach ($guidBytes in $currentMemberGuids)
{
$guid = [Guid]$guidBytes
$memberGuids.Add($guid)
}
# Get saved member GUIDs.
try
{
$savedMemberGuids = $Context.TargetObject.GetEx($savedMembersAttribute)
}
catch
{
if ($memberGuids.Count -eq 0)
{
return
}
# Save current member GUIDs and exit.
SaveCurrentMembers $currentMemberGuids $savedMembersAttribute
return
}
if (($savedMemberGuids.Length -eq 1) -and ($savedMemberGuids[0] -ieq "none"))
{
$savedMemberGuids = @()
}
# Find members that were removed from the group.
$removedMemberGuids = @()
foreach ($savedMemberGuid in $savedMemberGuids)
{
$guid = [Guid]$savedMemberGuid
if ($memberGuids.Remove($guid))
{
continue
}
$removedMemberGuids += $guid
}
if (($removedMemberGuids.Length -eq 0) -and ($memberGuids.Count -eq 0))
{
return
}
if ($memberGuids.Count -ne 0)
{
# Execute custom command on members that were added.
foreach ($newMemberGuid in $memberGuids)
{
# Bind to the member.
$path = "Adaxes://<GUID=$newMemberGuid>"
$newMember = $Context.BindToObjectEx($path, $True)
# Execute the custom command.
$newMember.ExecuteCustomCommand($customCommandIdAddedToGroup, $null)
}
}
if ($removedMemberGuids.Length -ne 0)
{
# Execute custom command on members that were removed.
foreach ($removedMemberGuid in $removedMemberGuids)
{
# Bind to the member
$path = "Adaxes://<GUID=$removedMemberGuid>"
$removedMember = $Context.BindToObjectEx($path, $True)
# Execute the custom command for removed members.
$removedMember.ExecuteCustomCommand($customCommandIdRemovedFromGroup, $null)
}
}
# Save current member GUIDs to custom attribute.
SaveCurrentMembers $currentMemberGuids $savedMembersAttribute
Comments 2
You must be signed in to comment.
Tim
Would it be possible to set this up as a business rule instead of a scheduled task?
Support
Hello Tim,
Yes, it is possible to run the script in a Business Rule configured for the Group object type. However, to trigger the rule corresponding changes must be made to a group via Adaxes. For example, if the script will be executed in a Business Rule triggering After adding a member to a group, it will trigger only when a member is added to a group included into the rule Activity Scope via Adaxes. If a member is added via any other tool (e.g. ADUC), the rule will not trigger.