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
Solved

Poweshell script to email the content of a folder

Posted on 2012-03-22
8
550 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
  • 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

791 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