Solved

PowerShell Script failing

Posted on 2014-09-18
3
47 Views
Last Modified: 2016-06-13
Hi Expert,

I need help here I'm using this PS script for sending email if any new files arrived in a folder, not sure why I can't get any email when adding files in folder and run the script.
Param (
	[string]$Path = "D:\ABC\",
	[string]$SMTPServer = "172.31.32.203",
	[string]$From = "abc.efg@abc.com.au",
	[string]$To = "abc.efg@abc.com.au",
	[string]$Subject = "New File Uploaded to FTP Site"
	)

$SMTPMessage = @{
    To = $To
    From = $From
	Subject = "$Subject at $Path"
    Smtpserver = $SMTPServer
}

$File = Get-ChildItem $Path | Where { $_.LastWriteTime -ge [datetime]::Now.AddMinutes(-1) }
If ($File)
{	$SMTPBody = "`nThe following files have recently been added/changed:`n`n"
	$File | ForEach { $SMTPBody += "$($_.FullName)`n" }
	Send-MailMessage @SMTPMessage -Body $SMTPBody
	
}

Open in new window


I added SMTP server and use the default IP address, and port 27. I open outbond connection in firewall for port 27.
Can someone help here.
Thanks in advance
0
Comment
Question by:adnankh
3 Comments
 
LVL 3

Accepted Solution

by:
Phil Bossman earned 500 total points
ID: 40331877
This code works for me out of the box.

Add some "Write-Verbose" code to find whats happening

Write-Verbose $File.FullName -Verbose

Open in new window

just after the $File assignment

Write-Verbose "Sending Email" -Verbose

Open in new window

Inside the If block

Open the powershell console,  cd to the script's path.  Add a file, then run the script and see what verbose output shows
0
 

Author Comment

by:adnankh
ID: 40331904
Cool Thanks I check it and back to you soon
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

I didn’t use eM Client for long when I decided to swap to Outlook 2016. The reason for the switch is that it started asking for payment to continue some of its services after one month.   The problems I faced when I didn’t pay were:   I was not …
Email signatures have numerous marketing benefits. Here are 8 top reasons to turn your email signature into a marketing channel.
The purpose of this video is to demonstrate how to set up Lists in Mailchimp. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchimp account. : Click on Lists. Click on Create List Button : Choose the desi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

912 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

18 Experts available now in Live!

Get 1:1 Help Now