среда, 31 июля 2019 г.

Скрипт для поиска однофамильцев в AD. powershell
Namesakes detecting in ActiveDirectory, powershell

#by me, 2019

import-module activedirectory

$SMTPSERVER = "exch.domain.local"

function main{
$users = (get-aduser -filter {enabled -eq $true -and EmailAddress -like "*@*"} -properties DisplayName).DisplayName
$doubleDisplayNames = $users | group | where{$_.count -gt 1}
if($doubleDisplayNames.count -gt 0) {
Send-MailMessage -From powershell@domain.local -To achernov@domain.local -Subject "Обнаружены полные однофамильцы в домене domain.local" -Body $($doubleDisplayNames.Name -join "`n") -SmtpServer $SMTPSERVER -Priority High -Encoding UTF8
}
}#end of main

try{
 main
}
catch
{
 $ErrorMessage = $_.Exception.Message
 Send-MailMessage -From powershell@domain.local -To admin@domain.local -Subject "Скрипт поиска однофамильцев в домене domain.loc вызвал ошибку выполнения." -Body $($ErrorMessage) -SmtpServer $SMTPSERVER -Priority High -Encoding UTF8
}