user2600700
user2600700

Reputation: 3

Array values in body of email on separate lines

I've put together a PS script that takes values from an AD group and puts them in an email to send to our helpdesk. The issue I am having is that the values from the AD group are in an array but I cannot figure out how to display those values on separate lines. Below is the code where I'm using an AD group with three testusers:

$members = @(Get-qADGroupMember adgroup -IncludedProperties "DisplayName,sAMAccountName")
$smtp = "smtpserver.domain.com" 
$to = "[email protected]"
$from = "[email protected]"
$subject = "Accounts Disabled"
$body = "Helpdesk, `n `n"
$body += "These users have had their passwords reset and their accounts disabled. There is no action required on the part of IT helpdesk, this is strictly to notify you.  These users will probably be calling soon: `n `n"

$body += For ($i=0; $i -lt $members.Length; $i++) {
    $members[$i -join "`n"]
}

$body += "`n"
$body += "For any questions regarding this, please contact the Information Security Team  `n `n"
$body += "Thank you `n"
$body += "Information Security"

This is the output:

Helpdesk,

These users have had their passwords reset and their accounts. There is no action required on the part of IT helpdesk, this is strictly to notify you. These users will probably be calling soon:

domain\testuser1 domain\testuser2 domain\testuser3 For any questions regarding this, please contact the Information Security Team

Thank you Information Security

How can I get the users from that group to be displayed on their own line like this?

domain\testuser1
domain\testuser2
domain\testuser3

Upvotes: 0

Views: 1508

Answers (1)

Chris Dent
Chris Dent

Reputation: 4240

You're over-working it. PowerShell likes to write lines. Since you already have an array here you can simple add to it.

$body = @("Helpdesk,") 
$body += "These users have had their passwords reset and their accounts disabled. There is no action required on the part of IT helpdesk, this is strictly to notify you. These users will probably be calling soon:"

$body += $members

Before you sent it, you only need convert it to a string.

Send-MailMessage -Body ($body | Out-String) <OtherParameters>

Upvotes: 2

Related Questions