Solved

PowerShell Script failing

Posted on 2014-09-18
3
52 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 AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This is my first article on Expert Exchange on the Manual Method of Exporting Office 365 Mailboxes to PST format by using the eDiscovery mechanism of Office. Hope you will enjoy the article.
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

825 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