Hi -
I'm sure this is totally elementary but I'm not seeing it. Hopefully my struggle can benefit other newbies. The issue I'm having is that I would like to send a different email based on success or failure of the transfer.
I have several PowerShell scripts running to automate uploads/downloads/and create archives. A couple scripts are sending email notifications using Office365.
In this case, I'm getting output on the screen but no email.
Here is part of the script.
# Connect
$session.Open($sessionOptions)
# Set up transfer options
$transferOptions = New-Object WinSCP.TransferOptions -Property @{
ResumeSupport = New-Object WinSCP.TransferResumeSupport -Property @{ State = [WinSCP.TransferResumeSupportState]::Off }
}
# Transfer files
$session.PutFiles("C:\SFTPUpload\itadmin\RRD\*", "/*", $False, $transferOptions).Check()
}
finally
{
$session.Dispose()
}
{
# Throw on any error
$transferResult.Check()
Send-MailMessage -From $From -To $To -Subject $SubjectGood -Credential ($Creds) -Body $BodyGood -SmtpServer $SMTPServer -UseSsl -Port 587
}
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
catch
{
Send-MailMessage -From $From -To $To -Subject $SubjectBad -Credential ($Creds) -Body $BodyBad -SmtpServer $SMTPServer -UseSsl -Port 587
}
Here is the output on the screen. I changed the destination to be read-only to force an error. So the output looks like what you would expect. But since I'm running from a scheduled task, I don't need or want the screen output, just the email.
***********Successful *****************
PS C:\Scripts> C:\Scripts\Test\Test KS 2.ps1
# Throw on any error
$transferResult.Check()
Send-MailMessage -From $From -To $To -Subject $SubjectGood -Credential ($Creds) -Body $BodyGood -SmtpServer $SMTPServer -UseSsl -Port 587
# Disconnect, clean up
$session.Dispose()
PS C:\Scripts>
**************Error *********************************
PS C:\Scripts> C:\Scripts\Test\Test KS 2.ps1
Exception calling "Check" with "0" argument(s): "Cannot overwrite remote file '/230425090309.txt'.$$
Press 'Delete' to delete the file and create new one instead of overwriting it.$$
Permission denied.
Error code: 3
Error message from server (en): Permission denied."
At C:\Scripts\Test\Test KS 2.ps1:46 char:5
+ $session.PutFiles("C:\SFTPUpload\itadmin\RRD\*", "/*", $False, $t ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SessionRemoteException
# Throw on any error
$transferResult.Check()