Link to home
Start Free TrialLog in
Avatar of GMC02108
GMC02108Flag for United States of America

asked on

Emailing a list of newly created AD users

I wrote the following script to send me the names of new users added to AD in the last day. It works fine as long as there was only one user added.

If none were added,  I get this:
Send-MailMessage : Cannot validate argument on parameter 'Body'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

If multiple were added, I get this:
Send-MailMessage : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Body'. Specified method is not supported.

If only one were added, I get an email that displays the same stuff you'd get if you just had this as your script:
$When = ((Get-Date).AddDays(-1)).Date
Get-ADUser -Filter {whenCreated -ge $When} -Properties whenCreated

(though all I really want is the Distinguished Name.

Could someone please help me update this script so that:
1) If no users were added, I just get an e-mail saying no new users in the body.
2) If one, or more users were added, I just get a list of their Distinguished Names in the body of the email.

Here's the script as it stands now:
$When = ((Get-Date).AddDays(-1)).Date
$body1 = Get-ADUser -Filter {whenCreated -ge $When} -Properties whenCreated
$Subject = "New Users Added in Last Day"


$AdminName = "me@me.com"
$Pass = Get-Content "\\mypath\cred.txt" | ConvertTo-SecureString
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $AdminName, $Pass

Send-MailMessage -To me@me.com -from $AdminName -Subject $Subject -Body $body1 -BodyAsHtml -smtpserver smtp.myserver.com -usessl -Credential $cred -Port 587


Thanks in advance!!
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GMC02108

ASKER

Thank you! Worked perfectly!
Just out curiosity, would there be a way to modify it so that it sent one e-mail per user, instead of putting them all in the same e-mail?
From a personal standpoint I hate the idea (the fewer emails the better in my opinion), but it's certainly possible.  You just have to loop through the results.
$AdminName = "me@me.com"
$Pass = Get-Content "\\mypath\cred.txt" | ConvertTo-SecureString
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $AdminName, $Pass
$Subject = "New Users Added in Last Day"

$When = ((Get-Date).AddDays(-1)).Date
If ( $body1 = Get-ADUser -Filter {whenCreated -ge $When} -Properties whenCreated | Select -ExpandProperty distinguishedName )
{
    $body1 | ForEach `
    {
        Send-MailMessage -To me@me.com -From $AdminName -Subject $Subject -Body $_ -SmtpServer smtp.myserver.com -UseSsl -Credential $cred -Port 587
    }
}
Else
{
    $body1 = "No new users"
    Send-MailMessage -To me@me.com -from $AdminName -Subject $Subject -Body $body1  -smtpserver smtp.myserver.com -usessl -Credential $cred -Port 587
}

Open in new window