Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

martin

Re: PowerShell Email Results

The code you have posted does not even seem syntactically correct to me. So I do not really understand it.
I'd suggest you to check the standard WinSCP example code here:
https://winscp.net/eng/docs/library_powershell#example
At the place, where it does exit 0, there you send the success email. And where it does exit 1, where you send the error email.
BigDog519

PowerShell Email Results

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()