Script repository
The script updates a user picture in the resource domain with that of the target user. To execute the script, create a business rule, custom command or scheduled task configured for the User object type. If secondary account update fails, an email notification will be sent to the address of the target user.
For the script to work, users must have the same username (sAMAccountName) or Full Name (cn) in the primary and secondary domains.
Parameters
$domainDN- the distinguished name (DN) of the secondary domain to update user picture in. For information on how to get an object DN, see Get the DN of a directory object.$subject- the subject of the email notification that will be sent to the user in case if secondary account update fails.$text- the text of the email notification that will be sent to the user in case if secondary account update fails.
$domainDN = "DC=domain,DC=com" # TODO: modify me
$subject = "Your Picture Update Was Unsuccessful" # TODO: modify me
$text = @"
Dear %fullname%,
Your picture update process was unsuccessful.
"@ # TODO: modify me
# Search user account in the resource domain.
try
{
$searcher = $Context.BindToObjectByDN($domainDN)
$searcher.Criteria = New-AdmCriteria "user" -Expression {(sAMAccountName -eq "%username%") -or (cn -eq "%fullname%")}
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
$searcher.SizeLimit = 2
# Execute search
$searchResultIterator = $searcher.ExecuteSearch()
$searchResults = $searchResultIterator.FetchAll()
if ($searchResults.Length -eq 0)
{
$Context.LogMessage("User %fullname% has no account in the secondary domain.", "Warning")
return
}
elseif ($searchResults.Length -gt 1)
{
$Context.LogMessage("Found more than one account for user %fullname% in the secondary domain", "Warning")
return
}
# Set the photo
$user = $Context.BindToObject($searchResults[0].AdsPath)
try
{
$picture = [byte[]]$Context.TargetObject.Get("thumbnailPhoto")
}
catch
{
# No photo
$picture = $NULL
}
$user.Put("thumbnailPhoto", $picture)
$user.SetInfo()
}
catch
{
# Send mail
$Context.SendMail("%mail%", $subject, $text, $NULL)
}
finally
{
# Release resources
$searchResultIterator.Dispose()
}
Comments 0
You must be signed in to comment.