formatting contents in the body of email

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
gferreri
Posts: 2
Last visit: Wed Apr 25, 2018 2:16 pm

formatting contents in the body of email

Post by gferreri »

Hi

I need to create a script that lists files from a folder and email to several people.

I wrote this line of code

$FileNames = Get-ChildItem -Path $failed_TXT_folder -File | Where-Object {$_.CreationTime -gt (Get-Date).AddDays(-35) }

to get a list of files from a folder defined in that variable $failed_TXT_folder with that where clause in it

I want the list of files to be 1 column to also include the $file.LastWriteTime

I know I can't simply write:

Send-MailMessage -From $from -To $emailTo -Subject $Subject -Body $FileNames -SmtpServer $Smtpserver

I think the -Body argument expects text and the elements of variable $FileNames are objects?

Thank you in advance

Gino
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: formatting contents in the body of email

Post by jvierra »

This will create a plain text body.

Code: Select all

$FileNames = Get-ChildItem -Path $failed_TXT_folder -File | 
      Where-Object {$_.CreationTime -gt (Get-Date).AddDays(-35) } |
      Select Fullname, LastWriteTime |
      Out-String
User avatar
gferreri
Posts: 2
Last visit: Wed Apr 25, 2018 2:16 pm

Re: formatting contents in the body of email

Post by gferreri »

Yep!

$FileNames = Get-ChildItem -Path $failed_TXT_folder -File | Where-Object {$_.CreationTime -gt (Get-Date).AddDays(-1)} | Out-String

Thanks

GF
This topic is 5 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked