Hello,
Here's the script that will do the job:
# Email message setings
$to = "%adm-InitiatorEmail%" # TODO: modify me
$subject = "My Subject" # TODO: modify me
$htmlReportHeader = @"
<h1><b>Full List of Subordinates for %name%</b></h1><br/>
<table border="1">
<tr>
<th>Manager Name</th>
<th>Manager Job Title</th>
<th>Employee Name</th>
<th>Employee Job Title</th>
</tr>
"@ # TODO: modify me
$htmlReportFooter = @"
</table><br/>
<p><i>Please do not reply to this e-mail, it has been sent to you for notification purposes only.</i></p>
"@ # TODO: modify me
function GetAllSubordinates($directReportDN, $subordinates, $managerName, $managerJobTitle)
{
if($subordinates.ContainsKey($directReportDN))
{
return
}
# Bind to Subordinate
$user = $Context.BindToObjectByDN($directReportDN)
# Get user name
$userName = $user.Get("name")
# Get user job title
try
{
$userJobTitle = $user.Get("title")
}
catch
{
$userJobTitle = " "
}
# Add to HashTable
$userInfo = New-Object PSObject
$userInfo | add-member Noteproperty ManagerName $managerName
$userInfo | add-member Noteproperty ManagerJobTitle $managerJobTitle
$userInfo | add-member Noteproperty UserName $userName
$userInfo | add-member Noteproperty UserJobTitle $userJobTitle
$subordinates.Add($directReportDN, $userInfo) | Out-Null
# Get Subordinates
try
{
$directReportDNs = $user.GetEx("directReports")
}
catch
{
return
}
foreach ($directReportDN in $directReportDNs)
{
GetAllSubordinates $directReportDN $subordinates $userName $userJobTitle
}
}
# Get subordinates
try
{
$directReportDNs = $Context.TargetObject.GetEx("directReports")
}
catch
{
$Context.LogMessage("The user doesn't have any direct reports.", "Error") # TODO: modify me
return
}
$subordinates = New-Object "System.Collections.Hashtable"
foreach ($directReportDN in $directReportDNs)
{
GetAllSubordinates $directReportDN $subordinates "%name%" "%title%"
}
# Sort list
$subordinates = $subordinates.Values | Sort-Object -Property @{Expression={$_.ManagerName}; Ascending=$True}
# Build HTML report
foreach ($subordinate in $subordinates)
{
$htmlReportHeader += "<tr><td>" + $subordinate.ManagerName + "</td>"
$htmlReportHeader += "<td>" + $subordinate.ManagerJobTitle + "</td>"
$htmlReportHeader += "<td>" + $subordinate.UserName + "</td>"
$htmlReportHeader += "<td>" + $subordinate.UserJobTitle + "</td></tr>"
}
$htmlBody = $htmlReportHeader + "</table>" + $htmlReportFooter
# Send the report
$Context.SendMail($to, $subject, $NULL, $htmlBody)
In the script:
- $to - specifies the recipient of the email notification. The %adm-InitiatorEmail% that is used in the script will be replaced with the email address of the user who triggered execution of the script. For example, if the script is executed with the help of a Custom Command, it will be replaced with the email address of the user who launched the Custom Command.
- $subject - specifies the subject of the email notification.
- $htmlReportHeader - specifies the header of the email notification.
- $htmlReportFooter - specifies the footer of the email notification.
Modify the script to your requirements.
To allow generating the report on demand with the help of a Custom Command:
- Create a new Custom Command.
- On the 2nd step of the Create Custom Command wizard, select the User object type.
- On the 3rd step, add the Run a program or PowerShell script action and paste the above script in the Script field.