Solved

Sending e-mail using Powershell 3.0

Posted on 2016-07-20
2
32 Views
Last Modified: 2016-07-21
Hello,

I am using powershell to detect which files are recently added to some predefined file locations. See next powershell code:

$time = (Get-Date).AddDays(-30)
$changes =  new-object System.Xml.XmlDocument
$changes.load("C:\test\locations.xml")
      $items = $changes.SelectNodes("//application") | select
      foreach ($item in $items)
        {
                  $location = $item.SelectSingleNode("./location") | % { $_.InnerText }
                  Get-ChildItem $location | `
            Where-Object {$_.LastWriteTime -gt $time}
        }

This files reads the locations defined in the c:\test\locations.xml file, and displays for each of these locations which files are added in the last 30 days.

Now, I want to e-mail this list. The next powershell command works fine when I want to send an e-mail:

Send-MailMessage -To "user@test.nl" -From "noreply@test.nl" -Subject "Test mail"  -Body "This is a test" -SmtpServer "SMTP server"

But now I want to e-mail the filelist. How can I manage to include the result of my first powershell script in the -Body of my second powershell script?

Regards,

Arne
0
Comment
Question by:adiemeer
2 Comments
 
LVL 40

Accepted Solution

by:
footech earned 500 total points
ID: 41721568
Essentially you need to gather all the file objects, grab the desired info (like the filename), and put form it into a single string.

Here's a basic example.  Other optimizations could be done depending on desired result, but this shows the concept.
$time = (Get-Date).AddDays(-30)
$changes =  new-object System.Xml.XmlDocument
$changes.load("C:\test\locations.xml")
$items = $changes.SelectNodes("//application") | select
$files = foreach ($item in $items)
{
    $location = $item.SelectSingleNode("./location") | % { $_.InnerText }
    Get-ChildItem $location | `
     Where-Object {$_.LastWriteTime -gt $time}
}
$body = $files | Select -ExpandProperty Name | Out-String
Send-MailMessage -To "user@test.nl" -From "noreply@test.nl" -Subject "Test mail"  -Body $body -SmtpServer "SMTP server"

Open in new window

0
 

Author Closing Comment

by:adiemeer
ID: 41723512
Thanks! This works fine!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
A brief introduction to what I consider to be the best editor for PowerShell.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question