Solved

Sending e-mail using Powershell 3.0

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

Accepted Solution

by:
footech earned 500 total points
Comment Utility
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
Comment Utility
Thanks! This works fine!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Set OWA language and time zone in Exchange for individuals, all users or per database.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now