Solved

Poweshell script to email the content of a folder

Posted on 2012-03-22
8
546 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
Does Powershell have you tied up in knots?

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

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

813 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

16 Experts available now in Live!

Get 1:1 Help Now