Solved

Poweshell script to email the content of a folder

Posted on 2012-03-22
8
551 Views
Last Modified: 2012-06-21
I have this script that will email be if a new file has arrvied in a folder.

Instead I would like it to email be the contents of the folder

if (Test-Path C:\Atlas_upload\*.xml)
{
$emailFrom = "gannet@test.co.uk"
$emailTo = "r.ben@test.co.uk"
$subject = "You have a new ATLAS file"
$body = "There is a new ATLAS file"
$smtpServer = "consignia"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
}


Thanks

Luke
0
Comment
Question by:luketr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 27

Accepted Solution

by:
KenMcF earned 250 total points
ID: 37751886
I would use the send-mailmessage cmdlet and do something liek this



$Attch = @()
Get-ChildItem C:\Atlas_Upload -Filter *.xml | Foreach {$Attch += $_.FullName}
Send-MailMessage -To User@Domain.local -From User@Domain.local -SmtpServer smtp.domain.local -Subject Test -Attachments $Attch
0
 
LVL 8

Expert Comment

by:Brent Challis
ID: 37755322
How are you triggering the code?  If the script is run on a schedule it will simply email all the xml files in the folder each time it runs.  I am assuming that after you receive the email you process the files and then delete them.  An alternative approach would be to use a file system watcher that would respond when a new xml file appears in the directory ond only process the new one.
0
 
LVL 9

Expert Comment

by:chrismerritt
ID: 37763617
Easiest way I find to email them is like this. Assumes your SMTP server supports SSL:

$MailSMTPServer = "SMTP Server Address"
$MailFrom = "sender@domain.com"
$MailRecipient = "recipient@domain.com"
$MailSubject = "Test"
$MailBody = "Test"
$AttachmentDirectory = "D:\Test\"

gci $AttachmentDirectory | % {$_.FullName} | Send-MailMessage -SmtpServer $MailSMTPServer -From $MailFrom -To $MailRecipient -Subject $MailSubject -UseSsl:$true -Body $MailBody

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:luketr
ID: 37764775
Hi Ken,

This code you kindly provided works fine. The only problem is when the files are processed it will still email me if there are no files in the directory.

$Attch = @()
Get-ChildItem C:\Atlas_Upload -Filter *.xml | Foreach {$Attch += $_.FullName}
Send-MailMessage -To User@Domain.local -From User@Domain.local -SmtpServer smtp.domain.local -Subject Test -Attachments $Attch

Thanks

Luke
0
 
LVL 8

Expert Comment

by:Brent Challis
ID: 37764879
Check $Attch.Count as being -gt 0 before sending the email.
0
 

Author Comment

by:luketr
ID: 37765247
I don't know if i'm doing this correct but I get an error

The term 'Check' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try aga
in.
At line:1 char:6
+ Check  <<<< $Attch.Count -gt 0


$Attch = @()
Get-ChildItem C:\Atlas_Upload -Filter *.xml | Foreach {$Attch += $_.FullName}
Check $Attch.Count -gt ()
$emailFrom = "gannet@test.gov.uk"
$emailTo = "test@test.gov.uk"
$subject = "You have a new ATLAS file"
$body = "$Attch"
$smtpServer = "consignia"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)

Thanks

Luke
0
 
LVL 9

Assisted Solution

by:chrismerritt
chrismerritt earned 250 total points
ID: 37765280
no, he means like this:

if ($Attch.Count -gt 0)
{
	#Process Code here
}

Open in new window

0
 
LVL 8

Expert Comment

by:Brent Challis
ID: 37765284
Here is the syntax for the test:

if ($Attch.Count -gt)
{
  $emailFrom = "gannet@test.gov.uk"
  $emailTo = "test@test.gov.uk"
  $subject = "You have a new ATLAS file"
  $body = "$Attch"
  $smtpServer = "consignia"
  $smtp = new-object Net.Mail.SmtpClient($smtpServer)
  $smtp.Send($emailFrom, $emailTo, $subject, $body)
}
else
{
  Write-Host "No files to process"
}
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

735 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