Hello Danny,
Here you are.
$to = "recipient@domain.com" # TODO: modify me
function GetObjectDisplayName($objectDN)
{
$objectPath = New-Object -TypeName "Softerra.Adaxes.Adsi.AdsPath" -ArgumentList @($null, $objectDN)
return [Softerra.Adaxes.Utils.ObjectNameHelper]::GetObjectName($objectPath, "IncludeParentPath")
}
function SearchObjects($baseObjectPath, $filter, $propertiesToLoad)
{
$searcher = $Context.BindToObject($baseObjectPath)
$searcher.PageSize = 500
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.SearchFilter = $filter
$searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
$searcher.SetPropertiesToLoad($propertiesToLoad)
$result = @{}
try
{
$searchResult = $searcher.ExecuteSearch()
$objects = $searchResult.FetchAll()
foreach ($objectID in $objects)
{
$properties = @{}
foreach ($propertyName in $propertiesToLoad)
{
if ($propertyName -eq "directReports")
{
$values = @()
foreach ($value in $objectID.Properties[$propertyName].Values)
{
$values += $value
}
$properties.Add($propertyName, $values) | Out-Null
}
else
{
$properties.Add($propertyName, $objectID.Properties[$propertyName].Value) | Out-Null
}
}
$result.Add($objectID.AdsPath, $properties)
}
return $result
}
finally
{
$searchResult.Dispose()
}
}
# Get the default Web Interface address
$webInterfaceAddress = "%adm-WebInterfaceUrl%"
$appendWebInterFaceLink = $True
if ([System.String]::IsNullOrEmpty($webInterfaceAddress))
{
$appendWebInterFaceLink = $False
$Context.LogMessage("Default web interface address not set for Adaxes service. For details, see http://www.adaxes.com/help/?HowDoI.ManageService.RegisterWebInterface.html", "Warning")
}
$htmlBuilder = New-Object "System.Text.StringBuilder"
$htmlBuilder.Append("<html><head>")
$htmlBuilder.Append("<meta http-equiv=""Content-Type""`
content=""text/html charset=UTF-8""></head>")
$htmlBuilder.Append("<body>")
$htmlBuilder.Append("<p>Disabled Managers</p>")
$htmlBuilder.Append("<table width=""100%%"" border=""1"">")
$htmlBuilder.Append("<tr>")
$htmlBuilder.Append("<th>Full Name</th><th>Username</th>
<th>Parent</th><th>Enabled Direct Reports</th>")
if ($appendWebInterfaceLink)
{
$htmlBuilder.Append("<th>Link</th>")
}
$htmlBuilder.Append("</tr>")
# Find disabled managers with direct reports
$managerFilter = "(&(sAMAccountType=805306368)(userAccountControl:1.2.840.113556.1.4.803:=2)(directReports=*))"
$propertiesToLoad = @("directReports", "distinguishedName", "name", "sAMAccountName", "objectGUID")
$disabledManagers = SearchObjects $Context.TargetObject.AdsPath $managerFilter $propertiesToLoad
$domainName = $Context.GetObjectDomain("%distinguishedName%")
$totalManagerCount = 0
foreach ($managerPath in $disabledManagers.Keys)
{
# Get direct reports
# Build filter
$managerProperties = $disabledManagers[$managerPath]
$filter = New-Object "System.Text.StringBuilder"
$filter.Append("(&(!(userAccountControl:1.2.840.113556.1.4.803:=2))(|")
foreach ($directReportDN in $managerProperties["directReports"])
{
$filter.Append([Softerra.Adaxes.Ldap.FilterBuilder]::Create("distinguishedName", $directReportDN)) | Out-Null
}
$filter.Append("))")
# Search enabled direct reports
$directReports = SearchObjects "Adaxes://$domainName/rootDSE" $filter.ToString() @("name", "sAMAccountName")
if ($directReports.Count -eq 0)
{
continue # Skip manager if all direct reports are disabled
}
# Add manager info to the report
$managerDN = New-Object "Softerra.Adaxes.Ldap.DN" $managerProperties["distinguishedName"]
$parentDisplayName = GetObjectDisplayName($managerDN.Parent.ToString())
$htmlBuilder.Append("<tr>")
$htmlBuilder.AppendFormat("<td>{0}</td>", $managerProperties["name"])
$htmlBuilder.AppendFormat("<td>{0}</td>", $managerProperties["sAMAccountName"])
$htmlBuilder.AppendFormat("<td>{0}</td>", $parentDisplayName)
# Add direct reports to the report
$htmlBuilder.Append("<td>")
foreach ($directReportPath in $directReports.Keys)
{
$directReportProperties = $directReports[$directReportPath]
$htmlBuilder.AppendFormat("{0} ({1})<br />", $directReportProperties["name"], $directReportProperties["sAMAccountName"])
}
$htmlBuilder.Append("</td>")
if ($appendWebInterfaceLink)
{
$htmlBuilder.AppendFormat("<td><a href='$webInterfaceAddress`ViewObject.aspx?guid={0}'>View</a></td>", [Guid]$managerProperties["objectGUID"])
}
$htmlBuilder.Append("</tr>")
$totalManagerCount++
}
$htmlBuilder.Append("</table>")
$htmlBuilder.AppendFormat("Total: {0} managers", $totalManagerCount)
$htmlBuilder.Append("</body></html>")
if ($totalManagerCount -ne 0)
{
$Context.SendMail($to, "[AD Report] Disabled Managers", $NULL, $htmlBuilder.ToString())
}