Hello,
Thank you for clarifying. You will need to use a Business Rule triggering After Creating a User and the below script.
$csvFilePath = "\\Server\share\file.csv" # TODO: modify me
$countryColumnName = "Country" # TODO: modify me
$jobTitleColumnName = "Title" # TODO: modify me
$groupIdentityColumnName = "Group" # TODO: modify me
function SearchObjects($filter)
{
$domainName = $Context.GetObjectDomain("%distinguishedName%")
$searcher = $Context.BindToObject("Adaxes://$domainName/rootDSE")
$searcher.SearchFilter = $filter
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.PageSize = 500
$searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
try
{
$searchResultIterator = $searcher.ExecuteSearch()
$searchResults = $searchResultIterator.FetchAll()
return ,$searchResults
}
finally
{
# Release resources
if ($searchResultIterator){ $searchResultIterator.Dispose() }
}
}
# Get user country
try
{
$country = $Context.TargetObject.Get("c")
}
catch
{
$Context.LogMessage("Country not specified", "Warning")
return
}
# Get user Job title
try
{
$title = $Context.TargetObject.Get("title")
}
catch
{
$Context.LogMessage("Job Title not specified", "Warning")
return
}
# Get group identity
$records = Import-Csv -Path $csvFilePath | Where{($_.$countryColumnName -eq $country) -and ($_.$jobTitleColumnName -eq $title)}
if ($records -ne $NULL)
{
foreach ($record in $records)
{
$groupIdentity = $record.$groupIdentityColumnName
$searchResults = SearchObjects "(&(objectCategory=group)(|(name=$groupIdentity)(distinguishedName=$groupIdentity)(sAMAccountName=$groupIdentity)))"
if ($searchResults.Length -eq 0)
{
$Context.LogMessage("Group '$groupIdentity' not found.", "Warning")
continue
}
elseif ($searchResults.Length -gt 1)
{
$Context.LogMessage("Found more than one group with the following identity '$groupIdentity'", "Warning")
continue
}
# Add user to the group
$group = $Context.BindToObject($searchResults[0].AdsPath)
$group.Add($Context.TargetObject.AdsPath)
}
}
In the script:
- $csvFilePath – Specifies the path to the CSV file;
- $countryColumnName – Specifies the name of the CSV file column that contains values for the Country property;
- $jobTitleColumnName - Specifies the name of the CSV file column that contains values for the Job Title property;
- $groupIdentityColumnName - Specifies the name of the CSV file column that contains groups the user should be added to.