PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, October 23, 2022

[FIXED] How to format Send-MailMessage body contents

 October 23, 2022     email, powershell     No comments   

Issue

I am working on a script that will ping a list of workstations and return add them to an array if they are offline. After they are added to an array the script then sends an email with the list of offline workstations. Here is the script so far

$list = Get-Content "C:\Users\$env:UserName\Desktop\workstations.txt"
ForEach ($_ in $list) { 
$test = Test-Connection -BufferSize 32 -Count 1 -ComputerName $_ -Quiet
IF ($test -match "false") {
$offlinelist += ($_)
}
}
Send-MailMessage -To "me <me@me.com>" -From "you <you@you.com>" -Subject "Offline Workstations" -SmtpServer "smtp.me.com" -body "These workstations are offline: <br> $offlinelist" -BodyAsHtml

In its current form the code will send an email with a output that looks like this:

These workstations are offline:
test1test2

What I need it to look like is this:

These workstations are offline:
test1
test2

I know there is a way to do it with possibly New-Object cmdlet or possibly the Format-Table cmdlet. I am just not sure how to go about structuring the code. I have found several examples, but I am not able to make it work with the Send-MailMessage cmdlet. Also, is it possible to stylize the body of the email with a different color font or face? Any help or example would be greatly appreciated. Thank you in advance.


Solution

In case anyone else comes across this in the future, I was able to solve my problem by adding a <br> after the workstation name in the array:

$offlinelist += "$_ <br>"

This seems to work because of the BodyAsHtml attribute in the Send-MailMessage cmdlet. I am not sure if this is the correct way syntax wise, but it is working for me without error. Cheers!



Answered By - Ozar
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing