Hello,
You need to replace the following code
$user = Get-AdmUser $user -Properties "LastLogonDate" `
-Server $domain -AdaxesService "localhost"
with this block:
$user = Get-AdmUser $user -Properties "LastLogonDate", "mailNickname", "homeMDB" `
-Server $domain -AdaxesService "localhost"
if ([System.String]::IsNullOrEmpty($user.mailNickname) -or [System.String]::IsNullOrEmpty($user.homeMDB))
{
continue
}
Find the full updated script below:
Import-Module Adaxes
$email = "recipient@company.com" # TODO modify me
$inactivityDurationThreshold = "30" # Days
$excludedOuDNs = @("CN=Users,DC=domain,DC=com", "OU=Sales,DC=domain,DC=com") # TODO modify me
$baseDN = "%distinguishedName%"
$domain = $Context.GetObjectDomain($baseDN)
function IsDescendantOfExludedOu ($userDN, $excludedOuDNs)
{
foreach ($ouDN in $excludedOuDNs)
{
if ($userDN.IsDescendantOf($ouDN))
{
return $True
}
}
return $False
}
function GetObjectDisplayName($objectDN)
{
$objectPath = New-Object -TypeName "Softerra.Adaxes.Adsi.AdsPath"`
-ArgumentList @($null, $objectDN)
return [Softerra.Adaxes.Utils.ObjectNameHelper]::GetObjectName(
$objectPath, "IncludeParentPath")
}
$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>")
$baseObjectDisplayName = GetObjectDisplayName($baseDN)
$htmlBuilder.appendFormat(
"<p>Inactive Users (<b>{0}</b>)</p>",
$baseObjectDisplayName)
$htmlBuilder.append("<table width=""100%%"" border=""1"">")
$htmlBuilder.append("<tr>")
$htmlBuilder.append("<th>User Name</th>
<th>Parent</th><th>Last Logon</th>")
$htmlBuilder.append("</tr>")
# Find inactive users
$users = Search-AdmAccount -AccountInactive `
-TimeSpan $inactivityDurationThreshold `
-SearchBase $baseDN -UsersOnly `
-Server $domain -AdaxesService localhost
if ($users)
{
foreach ($user in $users)
{
$user = Get-AdmUser $user -Properties "LastLogonDate", "mailNickname", "homeMDB" `
-Server $domain -AdaxesService "localhost"
if ([System.String]::IsNullOrEmpty($user.mailNickname) -or [System.String]::IsNullOrEmpty($user.homeMDB))
{
continue
}
$userDN = New-Object "Softerra.Adaxes.Ldap.DN" $user.DistinguishedName
if (IsDescendantOfExludedOu $userDN $excludedOuDNs)
{
continue
}
$parentDisplayName = GetObjectDisplayName($userDN.Parent.ToString())
$htmlBuilder.append("<tr>")
$htmlBuilder.appendFormat("<td>{0}</td>", $user.Name)
$htmlBuilder.appendFormat("<td>{0}</td>", $parentDisplayName)
$htmlBuilder.appendFormat("<td>{0}</td>", $user.LastLogonDate)
$htmlBuilder.append("</tr>")
}
}
$htmlBuilder.append("</table>")
$htmlBuilder.append("</body></html>")
$Context.SendMail($email, "[AD Report] Inactive Users", $NULL,
$htmlBuilder.ToString())