Solved

Sending e-mail using Powershell 3.0

Posted on 2016-07-20
2
35 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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
A brief introduction to what I consider to be the best editor for PowerShell.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scannerā€¦
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦

756 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