Email a list of new files (Powershell)

My goal here was to send myself an email of all new files received from vendors. This is assuming the files are not picked up and processed by BizTalk automatically. If that were the case, you might be able to point the folders to your archive directories and still achieve the same results.

In my particular case, we have a production serer exposed to our vendors. They send us test files, and I have to manually copy them and process them on our test server. At this time, we don’t have any access from one server to the other. So the email alert will trigger me to go process the files, rather than waiting for an email from the vendor.

<pre>
#
#      Name: Neal Walters
#      Date: 2019/07/29 
#
#   Purpose: Send email with all any new test files received. 
#            Files are received on Prod server that is exposed to outside world. 
#            We have to manually copy these files to our test system and process them. 
#
#

cls

$minutes = $args[0]

#allow this to be run 
if ($minutes -eq $null)
   {
     $minutes = 60
   }
   

$filterDate = (Get-Date).AddDays(-6) 
Write-Host "filterDate=$filterDate" 

$folderMask = "c:\BizTalk\Vendors\*\AS2FilesReceived\*.*"
#-Exclude "*997*.*"
$files = Get-ChildItem -Path $folderMask -File | Where-Object {$_.CreationTime -gt (Get-Date).AddMinutes($minutes)} | Sort-Object FullName
Write-Host "Number of Matching AS2 Files = $($files.Count)" 

$folderMask = "f:\SFTPFiles\*\*Test\From*To*\*.*"
$FTPfiles = Get-ChildItem -Path $folderMask -File | Where-Object {$_.CreationTime -gt (Get-Date).AddMinutes($minutes)} | Sort-Object FullName
Write-Host "Number of Matching SFTP Files = $($FTPfiles.Count)" 

$fileCount = 0 
$HTMLmessage = "<Table border=1>"

foreach ($file in $files) 
{
     $fileCount = $fileCount + 1 
     Write-Host "$fileCount $($file.FullName) $($file.CreationTime)`n" 
     $HTMLmessage = $HTMLMessage + "<tr><td>$fileCount $($file.FullName)</td><td> $($file.CreationTime)</td></tr>`n" 

}

foreach ($file in $FTPfiles) 
{
     $fileCount = $fileCount + 1 
     Write-Host "$fileCount $($file.FullName) $($file.CreationTime)`n" 
     $HTMLmessage = $HTMLMessage + "<tr><td>$fileCount $($file.FullName)</td><td> $($file.CreationTime)</td></tr>`n" 

}

$HTMLmessage = $HTMLMessage + "</Table>"

#[string[]] $toEmails = "nwalters@yourdomain.com", "bshaw@yourdomain.com" # List of toEmails to email your report to (separate by comma) or use a distribution list 
[string[]] $toEmails = "nwalters@yourdomain.com" # List of toEmails to email your report to (separate by comma) or use a distribution list 

$smtpServer = "smtp.yourdomain.com" 
$fromemail = "noreplyFilesReceived@yourdomain.com"
$emailSubject = "BizTalk EDI Files Received"

if ($fileCount -gt 0)
{
   send-mailmessage -from $fromemail -to $toEmails -subject $emailSubject  -BodyAsHTML -body $HTMLmessage -priority High -smtpServer $smtpServer
   $traceDateTime = get-date
   Write-Host "$traceDateTime Email sent to: $toEmails (size in bytes=$($HTMLmessage.length))" 
}

</pre>

Leave a Reply