We are using the dynamic dist list script found below. The issue is we also have to be able to provide overrides, which we are achieving through secondary static list we will maintain manually. When the script runs at 5 am EST every day it removes the override DL from the list. Is there a way to keep any overrides when the process runs?
$companyProperty = "adm-CustomAttributeText1" # TODO: modify me
$employeeTypeProperty = "adm-CustomAttributeText4" # TODO: modify me
function SearchObjects($filter, $domainName, $properties)
{
# Set search parameters
$searcher = $Context.BindToObject("Adaxes://$domainName")
$searcher.SearchFilter = $filter
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.PageSize = 500
$searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
$searcher.SetPropertiesToLoad($properties)
try
{
# Execute search
$searchResultIterator = $searcher.ExecuteSearch()
$searchResults = $searchResultIterator.FetchAll()
return ,$searchResults
}
finally
{
# Release resources
if ($searchResultIterator){ $searchResultIterator.Dispose() }
}
}
# Get company for LDAP filter
try
{
$company = $Context.TargetObject.Get($companyProperty)
}
catch
{
$Context.LogMessage("Company not specified", "Warning")
return
}
# Get employee type for LDAP filter
try
{
$employeeType = $Context.TargetObject.Get($employeeTypeProperty)
}
catch
{
$employeeType = $NULL
}
# Build filter
$filter = New-Object "System.Text.StringBuilder"
[void]$filter.Append("(&(sAMAccountType=805306368)(company=$company)")
if (-not([System.String]::IsNullOrEmpty($employeeType)))
{
[void]$filter.Append("(employeeType=$employeeType)")
}
[void]$filter.Append(")")
$domainName = $Context.GetObjectDomain("%distinguishedName%")
# Search users
$searchResults = SearchObjects $filter.ToString() $domainName @("distinguishedName")
# Add users to group
if ($searchResults.Length -eq 0)
{
$Context.TargetObject.PutEx("ADS_PROPERTY_CLEAR", "member", $NULL)
}
else
{
[System.Array]$userDNs = $searchResults | %%{$_.Properties["distinguishedName"].Value}
$Context.TargetObject.PutEx("ADS_PROPERTY_UPDATE", "member", $userDNs)
}
# Save the changes
$Context.TargetObject.SetInfo()