E-Mail issues with file Synchronization - PowerShell

Advertisement

msmithjr1375
Joined:
Posts:
5
Location:
Cincinnati, OH USA

E-Mail issues with file Synchronization - PowerShell

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
}

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
28,802
Location:
Prague, Czechia

Re: E-Mail issues with file Synchronization - PowerShell

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

Reply with quote

msmithjr1375
Joined:
Posts:
5
Location:
Cincinnati, OH USA

Re: E-Mail issues with file Synchronization - PowerShell

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.

Reply with quote

msmithjr1375
Joined:
Posts:
5
Location:
Cincinnati, OH USA

Re: E-Mail issues with file Synchronization - PowerShell

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.

Reply with quote

msmithjr1375
Joined:
Posts:
5
Location:
Cincinnati, OH USA

Re: E-Mail issues with file Synchronization - PowerShell

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.

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
28,802
Location:
Prague, Czechia

Re: E-Mail issues with file Synchronization - PowerShell

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/"

Reply with quote

msmithjr1375
Joined:
Posts:
5
Location:
Cincinnati, OH USA

Re: E-Mail issues with file Synchronization - PowerShell

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.

Reply with quote

Advertisement

You can post new topics in this forum