Script Repository


Add user to specified groups

May 18, 2020
316

The script adds a user to the groups whose names (Group Name (pre-Windows 2000)) are specified in the user property. Group names in the property must be separated by commas. To run the script, install Adaxes PowerShell Module for Active Directory on the computer where Adaxes service is running.

Edit Remove
PowerShell
Import-Module Adaxes

$propertyForGroupNames = "adm-CustomAttributeText1" # TODO: modify me

try
{
    $groupNames = ($Context.TargetObject.Get($propertyForGroupNames)).Split(",")
}
catch
{
    $Context.LogMessage("No groups specified.", "Warning") # TODO: modify me
    return
}

# Get user domain name
$domainName = $Context.GetObjectDomain("%distinguishedName%")

# Add user to groups
foreach ($groupName in $groupNames)
{
    try
    {
        Add-AdmGroupMember -Identity $groupName.Trim() -AdaxesService localhost -Members "%username%" -Server $domainName -ErrorAction Stop
    }
    catch
    {
        $Context.LogMessage("An error occurred while adding user to group $groupName. Error: " + $_.Exception.Message, "Error")
    }
}

# Clear group names proeprty
$Context.TargetObject.Put($propertyForGroupNames, $NULL)
$Context.TargetObject.SetInfo()


Comments ( 0 )
No results found.
Leave a comment

Related Scripts