Solved

Poweshell script to email the content of a folder

Posted on 2012-03-22
8
544 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:bchallis
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
 

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
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

 
LVL 8

Expert Comment

by:bchallis
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:bchallis
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Synchronize a new Active Directory domain with an existing Office 365 tenant
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

12 Experts available now in Live!

Get 1:1 Help Now