I figured it out the error. I was calling Win32::OLE->Option(Warn => sub {goto CheckError}); from the sub routine which was calling the session->Open. I had to move that out of the sub routine to capture the errors.
i.e.
Win32::OLE->Option(Warn => sub {goto CheckError});
sub sftp{
Win32::OLE->Initialize(Win32::OLE::COINIT_OLEINITIALIZE);
my $session = Win32::OLE->new('WinSCP.Session') || die "Can't create winscp sessoion: ", Win32::OLE->LastError;
my $consts = Win32::OLE::Const->Load($session);
my $sessionOptions = Win32::OLE->new('WinSCP.SessionOptions')|| die "Can't open winscp
sessoionOptions: ", Win32::OLE->LastError;
$sessionOptions->{'Protocol'} = $consts->{'Protocol_Sftp'};
$sessionOptions->{'HostName'} = $Server;
$sessionOptions->{'UserName'} = $UserId;
$sessionOptions->{'Password'} = $SSHPassword;
$sessionOptions->{'SshHostKeyFingerprint'} = $SSHHostKeyFingerPrint;
$session->Open( $sessionOptions);
my $OLEConnectionError = Win32::OLE->LastError;
if ("$OLEConnectionError" ne "0")
{
print "$OLEConnectionError","\n";
exit 13;
}
}
i.e.
Win32::OLE->Option(Warn => sub {goto CheckError});
sub sftp{
Win32::OLE->Initialize(Win32::OLE::COINIT_OLEINITIALIZE);
my $session = Win32::OLE->new('WinSCP.Session') || die "Can't create winscp sessoion: ", Win32::OLE->LastError;
my $consts = Win32::OLE::Const->Load($session);
my $sessionOptions = Win32::OLE->new('WinSCP.SessionOptions')|| die "Can't open winscp
sessoionOptions: ", Win32::OLE->LastError;
$sessionOptions->{'Protocol'} = $consts->{'Protocol_Sftp'};
$sessionOptions->{'HostName'} = $Server;
$sessionOptions->{'UserName'} = $UserId;
$sessionOptions->{'Password'} = $SSHPassword;
$sessionOptions->{'SshHostKeyFingerprint'} = $SSHHostKeyFingerPrint;
$session->Open( $sessionOptions);
my $OLEConnectionError = Win32::OLE->LastError;
if ("$OLEConnectionError" ne "0")
{
print "$OLEConnectionError","\n";
exit 13;
}
}