With the help of WINScp's site, I was able to create a powershell script for downloading files from an sftp site. Two files are uploaded and are named: F_Report_123456_0986.csv and S_Report_123456_0986.csv with the numbers changing every time a new file is uploaded.
I am looking for guidance on cleaning up the script. Currently it is working as a scheduled task, but I'm sure the scripting could be better. I'm new to PS and am learning a lot as I go. I'd also like help on the body of the email. I worked on it and was able to get the "send-mailmessage" part to work on it's own, but when I incorporated it into the script, it wouldn't run. I would like for the body to say, "Please see attached files, F_PartReport.csv dated <and for the date to be the file date> and S_PartReport.csv dated <same>."
Any advice, guidance and/or help would be very much appreciated. Thank you!
param (
$localPath = "C:\PartReport\Today\*.*",
$remotePath = "/incoming/Export/Production/Output/"
)
try
{
#Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
#Delete files before running script
Remove-item "C:\PartReport\Yesterday\*.*"
#Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "ftp.name.com"
UserName = "username"
Password = "password"
SshHostKeyFingerprint = "fingerprint key here"
}
$session = New-Object WinSCP.Session
try
{
#Connect
$session.Open($sessionOptions)
#Downloads files to the local directory $localPath
$transferResult = $session.GetFiles("$remotePath", "$localPath", $False, $transferOptions)
#Throw on any error
$transferResult.Check()
#Get-items variables
$temp = Get-Item C:\PartReport\Today\F_PartReport*.csv
$temp2 = Get-Item C:\PartReport\Today\S_PartReport*.csv
#Renames the F_PartReport*.csv file to F_PartReport.csv
Rename-Item $temp F_PartReport.csv
#Renames the S_PartReport*.csv file to S_PartReport.csv
Rename-Item $temp2 S_PartReport.csv
#Sends email
Send-MailMessage -To "multiple_recipients@mail.com" -From "sender@mail.com" -smtpserver smtp.mail.com -Subject "Reports" -Body "Please see attached files." - Attachments "C:\PartReport\Today\F_PartReport.csv","C:\PartReport\Today\S_PartReport.csv"
#Moves files
Move-item C:\PartReport\Today\*.csv C:\PartReport\Yesterday
#Renames files to add date to file name
Rename-Item C:\PartReport\Yesterday\F_PartReport.csv "F_PartReport.csv_$(get-date -f MM-dd-yyyy).csv"
Rename-Item C:\D2L-ParticipationReport\Yesterday\S_PartReport.csv "S_PartReport.csv_$(get-date -f MM-dd-yyyy).csv"
#Removes files from SFTP site
$removeFiles = "/incoming/Export/Production/Output/*.*"
$Session.RemoveFiles("$removeFiles")
}
finally
{
#Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Output $_.Exception.Message
exit 1
}
EMAIL PART OF SCRIPT - When I run this by itself, it works without an issue, but when I place it in the script above, replacing the #send email portion of it, it throws an error and does not work.
#Email variables to retrieve file names, dates and time
$facbodyname = Get-Item C:\PartReport\Today\F_PartReport.csv | Foreach-object {$_.name}
$stubodyname = Get-Item C:\PartReport\Today\S_PartReport.csv| Foreach-object {$_.name}
$facbodytime = Get-Item C:\PartReport\Today\F_PartReport.csv | Foreach-object {$_.LastWriteTime.ToString()}
$stubodytime = Get-Item C:\PartReport\Today\S_PartReport.csv | Foreach-object {$_.LastWriteTime.ToString()}
$toemail = "recipient1@mail.com", "recipient2@mail.com"
$fromemail = "sender@mail.com"
$smtp = "smtp.mail.com"
$subject = "Reports"
$body = "Please see the attached files, $facbodyname dated $facbodytime and $stubodyname dated $stubodytime"
$attach = "C:\PartReport\Today\F_PartReport.csv","C:\PartReport\Today\S_PartReport.csv"
Send-MailMessage -To $toemail -From $fromemail -smtpserver $smtp -Subject $subject -Body $body -Attachments $attach