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: Notification on script error

Sorry, but sending an email from PowerShell has nothing to do with WinSCP.
Please, ask at an appropriate site, like Stack Overflow.
Or see for example https://stackoverflow.com/q/41938006/850848
lvance18

Notification on script error

I have a script that will download all the files with today's date. I need to send out an email notification when this script throws an error. I am new to scripting and cannot get these notifications to work. Please take a look and let me know my error.

# Load WinSCP .NET assembly

Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"

# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "test"
    UserName = "test"
    Password = "test"
    SshHostKeyFingerprint = "ssh-rsa 1024 test"
    Timeout = New-TimeSpan -Minutes 10
}

$session = New-Object WinSCP.Session

try
{
    # Connect
    $session.Open($sessionOptions)

    $sessionOptions.AddRawSettings("SendBuf", "0")
    $sessionOptions.AddRawSettings("SshSimple", "0")


    # Set up transfer options
    $transferOptions = New-Object WinSCP.TransferOptions
    $transferOptions.FileMask = "*>=1D"
   
    # Transfer files
    $session.GetFiles("test/*.txt", "\\Test Scripting\*", $False, $transferOptions).Check()
}


finally

{
    $session.Dispose()
}

if( $Error -eq 0 ){
    $FailMailParams = @{
        To = ''
        From = ''
        Port = '25'
        SmtpServer = ''
        Subject = 'Script Errors Out'
        Body = 'There was an error with the script!!'
        }
       }