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!!
GMC02108Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
Haven't had a chance to test, but I think this will work. I removed the HTML switch from Send-MailMessage since there wasn't really any need for it (you would need to generate full HTML code for the body for it to make sense).
$When = ((Get-Date).AddDays(-1)).Date
If ( $body1 = Get-ADUser -Filter {whenCreated -ge $When} -Properties whenCreated )
{ $body1 = $body1 | Select distinguishedName | Out-String }
Else
{ $body1 = "No new users" }
$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  -smtpserver smtp.myserver.com -usessl -Credential $cred -Port 587

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GMC02108Author Commented:
Thank you! Worked perfectly!
GMC02108Author Commented:
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?
footechCommented:
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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.