Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sending e-mail using Powershell 3.0

Posted on 2016-07-20
2
Medium Priority
?
48 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 41

Accepted Solution

by:
footech earned 2000 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Loops Section Overview
Screencast - Getting to Know the Pipeline

963 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