Script repository
The scripts transfer subordinates from the user to their assistant and back. To execute the scripts, create a business rule, scheduled task or custom command configured for the User object type.
Script 1: Transfer subordinates from user to assistant
Except for transferring subordinates to the user assistant, the script also preserves the current manager GUID in CustomAttributeBinary1. The preservation will be used by the second script to bring subrodinates back to the user.
# Get subordinates.
try
{
$subordinateDNs = $Context.TargetObject.GetEx("directReports")
}
catch
{
$Context.LogMessage("The user has no subordinates", "Information")
return
}
# Transfer subordinates to assistant.
$currentManagerGuidByte = $Context.TargetObject.Get("objectGuid")
foreach ($subordinateDN in $subordinateDNs)
{
$subordinate = $Context.BindToObjectByDN($subordinateDN)
# Save former manager GUID in CustomAttributeBinary1
$subordinate.Put("adm-CustomAttributeBinary1", $currentManagerGuidByte)
# Update manager
$subordinate.Put("manager", "%assistant%")
# Save changes
$subordinate.SetInfo()
}Script 2: Transfer subordinates back to the user
The script will revert the changes made by the previous script and return subordinates back to the original manager. When run on a user account, the script will find all users who are currently managed by the user’s assistant, and those of them who have the target user’s GUID specified in the CustomAttributeBinary1 attribute will be assigned as direct reports of the target user.
# Get user assistant.
try
{
$currentManagerDN = $Context.TargetObject.Get("assistant")
}
catch
{
$Context.LogMessage("No replacement manager specified for this user", "Information")
return
}
# Bind to the assistant.
$currentManager = $Context.BindToObjectByDN($currentManagerDN)
# Get assistant subordinates.
try
{
$subordinateDNs = $currentManager.GetEx("directReports")
}
catch
{
$Context.LogMessage("User '" + $currentManager.Get("name") + "' has no subordinates.", "Information")
return
}
# Transfer subordinates.
$targetUserGuidBytes = $Context.TargetObject.Get("objectGuid")
$targetUserGuid = New-Object "System.Guid" (,$targetUserGuidBytes)
foreach ($subordinateDN in $subordinateDNs)
{
$subordinate = $Context.BindToObjectByDN($subordinateDN)
# Get former manager GUID.
try
{
$formerManagerGuidBytes = $subordinate.Get("adm-CustomAttributeBinary1")
}
catch
{
continue
}
$formerManagerGuid = New-Object "System.Guid" (,$formerManagerGuidBytes)
if (!($targetUserGuid.Equals($formerManagerGuid)))
{
continue
}
# Clear custom attribute.
$subordinate.Put("adm-CustomAttributeBinary1", $NULL)
# Assign target user as manager.
$subordinate.Put("manager", "%distinguishedName%")
$subordinate.SetInfo()
}
Comments 0
You must be signed in to comment.