Send output of "Search-ADAccount" as body variable in SEND-MailMessage

Hello,

I have a script that searches the user accounts that have not been logged on to in 30 days and email the results to a group.

The script works except that I’m using the OUT-STRING to a $BODY variable for the SEND-MailMessage and that sends it as a long string rather than a column list.

How can I send it as column list?

Script:
#Find users that have not logged on in 30 days
$a = Search-ADAccount -AccountInactive -UsersOnly -TimeSpan 30.00:00:00 -SearchBase “OU=Accounts,DC=HQ,DC=Company,DC=Com” | ? {$.DistinguishedName -notlike “OU=Services,OU=Accounts,” -and $.DistinguishedName -notlike “OU=Shared,OU=Accounts,” -and $.DistinguishedName -notlike “OU=Inactive,OU=Accounts,” -and $.DistinguishedName -notlike “OU=Special,OU=Accounts,”}

$b = $a | select Name

#Variables
$from = "IT Dept. "
$emailaddress = “AdminGroup@company.com
$body = Write-Output $b | Out-String
$smtpserver = “mail”

#Send Email Message
Send-Mailmessage -smtpServer $smtpServer -from $from -to $emailaddress -subject $subject -Body $body -BodyAsHtml -priority High

Look at Convertto-HTML as a way to do this

$html = Convertto-Html -Body "$a"

$body = $html | out-string

Send-Mailmessage -smtpServer $smtpServer -from $from -to $emailaddress -subject $subject -Body $body -BodyAsHtml -priority High

Thank you Wei-Yen. I’ll try that out.

I actually found another way but the OU I’m referencing has sub-OUs that contain service or shared accounts that where created in Office 365 for a special purpose but never get logged on - so far script only identified 1 account.

My manager know wants to see those “known” or “whitelisted” accounts displayed in the email under a heading “Known accounts” or “White listed accounts”

But, not sure how to go about scripting that. I don’t know if I can do another “ELSE” statement.
Here is the script that’s currently running:
######################################################################

Please Configure the following variables…

$smtpServer=“mail”
$from = "Contoso IT "
$emailaddress = “italerts@Contoso.com
$subject = “Contoso AD users that have not logged on in 30 days”
[string]$body = " "

#######################################################################

#Find all AD users that have not logged on in 30 days.
#######################################################################
#The SEARCHBASE starts in “OU=Accounts” but excludes the following OUs:
#Service, Shared, Inactive, Special and NDR account.
#######################################################################

$accounts = Search-ADAccount -AccountInactive -UsersOnly -TimeSpan 30.00:00:00 -SearchBase “OU=Accounts,DC=HQ,DC=Contoso,DC=Com” | ? {$.DistinguishedName -notlike “OU=Services,OU=Accounts,” -and $.DistinguishedName -notlike “OU=Shared,OU=Accounts,” -and $.DistinguishedName -notlike “OU=Inactive,OU=Accounts,” -and $.DistinguishedName -notlike “OU=Special,OU=Accounts,” -and $_.DistinguishedName -notlike “CN=NDR Account*”}

If ($accounts -eq $null) {

Send-Mailmessage -smtpServer $smtpServer -from $from -to $emailaddress -subject $subject -Body “No users found.” -priority High

}

Else {

foreach ($account in $accounts) {
$body = $body + $account.name + “`r`n”}

Send-Mailmessage -smtpServer $smtpServer -from $from -to $emailaddress -subject $subject -Body $body -priority High
}

In the above script, I’m excluding the “NDR account” from being reported on. How can I still report that known account in the email under a heading “Known accounts” in the body of the email?

Thanks.