Home > Uncategorized > Get List of AD Users by Email not logged in since 90 Days with some Users excluded

Get List of AD Users by Email not logged in since 90 Days with some Users excluded

$Header = @”
<style>
TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
TH {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: #C1C4FA; }
TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
</style>
“@
$preContent = “<h2>$env:computername</h2>
<h3>MyDomain AD Users not logged-in last 90 Days .</h3>

$postContent = “<br><b>NOTE :</b> <br> This list does not include disabled and some service accounts. </br>”
$DaysInactive = 90
$time = (Get-Date).Adddays(-($DaysInactive))

# Get AD Users with lastLogonTimestamp less than time specified and is enabled
$htmlbody = Search-ADAccount -UsersOnly -AccountInactive -TimeSpan 30.00:00:00 | Select-Object Name,SAMAccountName,LastLogonDate,Enabled |
Where-Object { $_.Enabled -eq $true -and $_.samAccountName -notlike ‘sanner’ -and $_.samAccountName ‘MSOL_79fdadasda91’} |
sort-object lastlogondate | ConvertTo-Html -PreContent $preContent -PostContent $postContent -Property Name,LastLogonDate,Enabled -Head $Header | out-string

$post = “<BR><i>Report generated on $((Get-Date).ToString()) from $($Env:Computername)</i>”

Send-MailMessage -Subject “Users not loggedin last 30 Days | mydomain.local” -to ITAdmin@myDomain.local -from no-reply@myDomain.local -Body $htmlbody -SmtpServer “192.168.1.1” –BodyAsHtml

 

Entire Powershell script look like this :

ad1_all_scriptEmail will look like this :

email_aduser

If All works fine you can configure this in Windows Task scheduler to be run Automatically.

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: