Topic "E-Mail issues with file Synchronization - PowerShell"

Author Message
msmithjr1375
[View user's profile]

Joined: 2017-07-20
Posts: 5
Location: Cincinnati, OH USA
I have a script that works great, except for everyday at the same exact time it sends out 900+ separate e-mails of each file that has been transferred over to our local server. I have no clue why it is doing this and I thought that synchronization feature looks for only new files only.

Here is a sample script (sensitive data removed). The story with it is that our customer wants a e-mail confirmation of each file (separately) that we have downloaded. They typically send 1 to 20 files each day. Any idea or thoughts on how to prevent it from sending all of these e-mails each day?

try
{
# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::FTP
HostName = "e-mail address here"
UserName = "user"
Password = "password"

}

# Account used for secure e-mail account
$secpasswd = ConvertTo-SecureString "uMs@2015" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential (“umservice@unitedmail4.onmicrosoft.com”, $secpasswd)
#$fileIncludes = @("*.txt","*.csv","*.zip","*.xml","*.xlsx","*.pdf","*.xls","*.doc","*.docx")


# Setup e-mail info
$SMTPMessage = @{From="umservice@unitedmail4.onmicrosoft.com"
To="msmith@united-mail.com","cabner@united-mail.com", "jmarshall@united-mail.com", "mscuglik@united-mail.com"
Subject="Level One File Received"
SmtpServer="smtp.office365.com"
Credential=$mycreds
Port="587"}


$session = New-Object WinSCP.Session

$remotePath = "remote FTP on Sharefile"
$localPath = "local server"
try
{
# Connect
$session.Open($sessionOptions)


# Download files
$transferOptions = New-Object WinSCP.TransferOptions
#$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferOptions.FileMask = "*>=1D"
$transferOptions.FileMask = "specific folders to ignore"




$synchronizationResult =
$session.SynchronizeDirectories(
[WinSCP.SynchronizationMode]::local, "$localPath", "$remotePath", $False,$False,[WinSCP.SynchronizationCriteria]::Time,$transferOptions)
# Throw on any error to emulate the (default) "option batch abort" mode
$synchronizationResult.Check()


foreach ($transfer in $synchronizationResult.Downloads)
{

$file = Split-Path $transfer.FileName -Leaf


If ($file){
$SMTPBody ="We have received your file and will begin processing as soon as possible.`n`n"
$SMTPBody +="$file`r`n"
Send-MailMessage -UseSsl @SMTPMessage -Body $SMTPBody}

}


}

finally
{
# Disconnect, clean up
$session.Dispose()

}

exit 0
}
catch [Exception]
{
Write-Host ("Error: {0}" -f $_.Exception.Message)
exit 1
}
Advertisements
martin◆
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 26705
Location: Prague, Czechia
Please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate the session log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.
_________________
Martin Prikryl
msmithjr1375
[View user's profile]

Joined: 2017-07-20
Posts: 5
Location: Cincinnati, OH USA
martin wrote:
Please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate the session log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.


Thanks for your reply. Attached is the log file. I did remove our remote server path and replaced it with [REMOTE SERVER]. When this log was generated it did fire off a bunch of e-mails.
LevOne.log (4.23 KB) [Download]

Description: (none)

msmithjr1375
[View user's profile]

Joined: 2017-07-20
Posts: 5
Location: Cincinnati, OH USA
msmithjr1375 wrote:
martin wrote:
Please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate the session log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.


Thanks for your reply. Attached is the log file. I did remove our remote server path and replaced it with [REMOTE SERVER]. When this log was generated it did fire off a bunch of e-mails.


Well I know what the issue is. We have another script on a different server that cleans up old files out of the same directory as the one I am downloading files to. So it runs at 8:20AM each day after that my script runs and re-downloads the files it has deleted. So I do have a 24hr mask set on this script. How can I get this to work so it does not pull in old files? I attempted to do a move after the file download, but that just fails.
msmithjr1375
[View user's profile]

Joined: 2017-07-20
Posts: 5
Location: Cincinnati, OH USA
Well good news, I figured out the issue. For some reason the FileMask for the last day would not work if I had the FileMask with the folders I wanted the sync to ignore. Once I commented it out, it worked right away. It's no big deal though, nobody puts anything in those folders anyway.
martin◆
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 26705
Location: Prague, Czechia
msmithjr1375 wrote:
Well good news, I figured out the issue. For some reason the FileMask for the last day would not work if I had the FileMask with the folders I wanted the sync to ignore. Once I commented it out, it worked right away. It's no big deal though, nobody puts anything in those folders anyway.

OK, I see. Obviously, if you re-assign the FileMask property, the previous value is lost. As with any other property.

You have to merge the masks:
$transferOptions.FileMask = "*>=1D|folder_to_exclude1/;folder_to_exclude2/"
msmithjr1375
[View user's profile]

Joined: 2017-07-20
Posts: 5
Location: Cincinnati, OH USA
martin wrote:
msmithjr1375 wrote:
Well good news, I figured out the issue. For some reason the FileMask for the last day would not work if I had the FileMask with the folders I wanted the sync to ignore. Once I commented it out, it worked right away. It's no big deal though, nobody puts anything in those folders anyway.

OK, I see. Obviously, if you re-assign the FileMask property, the previous value is lost. As with any other property.

You have to merge the masks:
$transferOptions.FileMask = "*>=1D|folder_to_exclude1/;folder_to_exclude2/"


Thanks for the info, I will fix that in my code. I really enjoy working with WinSCP, it is extremely powerful and useful for the company I work for. We always purchased WS_FTP, nice and easy to work with, but it really lacks the capabilities that WinSCP has.
Advertisements

You can post new topics in this forum

Search

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License