Solved

Poweshell script to email the content of a folder

Posted on 2012-03-22
8
545 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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.
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

867 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

14 Experts available now in Live!

Get 1:1 Help Now