We use cookies to improve your experience.
By your continued use of this site you accept such use.
For more details please see our privacy policy and cookies policy.

Script Repository

Add members of the target group to the specified one

April 14, 2022 Views: 352

The script adds users who are members of the target group to the specified one. The script can be executed in a custom command, business rule or scheduled task configured for the Group obejct type.

Parameters:

  • $targetGroupDN - Specifies the distinguished name (DN) of the group to add members of the target group to. For information on how to get an object DN, see http://adaxes.com/sdk/HowDoI.GetDnOfObject/.
  • $pipelined - Specifies whether to add members to the group through Adaxes pipeline to create log records, apply business rules, security roles, etc.
Edit Remove
PowerShell
$targetGroupDN = "CN=My Group,OU=Groups,DC=domain,DC=com" # TODO: modify me
$pipelined = $True # TODO: modify me

$targetGroup = $Context.BindToObjectEx("Adaxes://$targetGroupDN", $pipelined)

$searcher = $Context.TargetObject
$searcher.SearchFilter = "(sAMAccountType=805306368)"
$searcher.SearchScope = "ADS_SCOPE_BASE"
$searcher.PageSize = 500
$searcher.AttributeScopeQuery = "member"

try
{
    # Execute search
    $searchIterator = $searcher.ExecuteSearch()
    $searchResults = $searchIterator.FetchAll()
    
    if ($searchResults.Length -eq 0)
    {
        $Context.LogMessage("There are no members to copy.", "Information")
        return
    }
    
    foreach ($searchResult in $searchResults)
    {
        if ($targetGroup.IsMember($searchResult.AdsPath))
        {
            continue
        }
        
        $targetGroup.Add($searchResult.AdsPath)
    }
}
finally
{
    # Release resources
    if ($searchIterator){ $searchIterator.Dispose() }
}
Comments 0
Leave a comment
Loading...

Got questions?

Support Questions & Answers