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
}
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
}