New Server Latest WinSCP, Perl Logic Not Working, Looking for Assistance

Advertisement

jpantera
Joined:
Posts:
26
Location:
Los Angeles, CA

New Server Latest WinSCP, Perl Logic Not Working, Looking for Assistance

Hello Martin & WinSCP Support,

I have a series of Perl scripts on some servers that I'm replacing, with the latest version of WinSCP, minimal code changes.

I'm running into an issue with scripts that use the .Net Assembly, and looking for your assistance.

The following logic works on the old servers, but I'm getting errors with the new servers, after registering the .NET Assembly 32-bit and 64-bit objects, per https://winscp.net/eng/docs/library_install "Registering for COM":

Code, with lines where the errors occur repeated at end, with their errors, seem to maybe point to a syntax change needed somewhere?

I did notice that on the examples on the WinSCP site, the former Perl example to enumerate files does not appear to be there any longer (only PowerShell, VB, others).

CODE:
Win32::OLE->Initialize(Win32::OLE::COINIT_OLEINITIALIZE);
my $session = Win32::OLE->new('WinSCP.Session') || warn "Can't create winscp session: ", Win32::OLE->LastError;
my $consts = Win32::OLE::Const->Load($session);
my $sessionOptions = Win32::OLE->new('WinSCP.SessionOptions')|| die "Can't open winscp session Options: ", Win32::OLE->LastError;
$sessionOptions->{'Protocol'} = $consts->{'Protocol_Sftp'};
$sessionOptions->{'HostName'} = $ProgramRemoteSFTPSettings::RemoteSFTPServer;
$sessionOptions->{'UserName'} = $ProgramRemoteSFTPSettings::USER;
$sessionOptions->{'Password'} = $ProgramRemoteSFTPSettings::PASSWORD;
$sessionOptions->{'SshHostKeyFingerprint'} = $ProgramRemoteSFTPSettings::DestServerFingerPrintKey;
 
$session->Open($sessionOptions);
my $directory = 
$session->ListDirectory("${ProgramRemoteSFTPSettings::RemoteSFTPDirectory}\/");
my $items = Win32::OLE::Enum->new($directory->{'Files'});
my $item;
while (defined($item = $items->Next))
 {
  my $FilePermissionsHash = $item->{'FilePermissions'};
  my $FilePermissions = $FilePermissionsHash->{'Text'};
  print "DEBUG\:\t\-\-  File Name on SFTP Site \= " . $item->{'Name'} . "\n\-\-  FILE DETAIL on SFTP Site \:\n    File Length\: " . $item->{'Length'} . "\, File Permissions\: " . $FilePermissions . "\, File Last Write Mod Date\: " . $item->{'LastWriteTime'} . "\n";
  if (($item->{'Name'} =~ /FileName01\_([^#]+)\.csv/i) || ($item->{'Name'} =~ /FileName02\_([^#]+)\.csv/i))
   {
    push (@FileContent::FilesOnSFTPSite,"$item->{'Name'}");
    push (@FileContent::FilesOnSFTPSiteModTime, "$item->{'LastWriteTime'}");
    print "DEBUG\:\tFILE = <$item->{'Name'}> Will be reviewed to see if it has been downloaded before\.\n";
    if (($item->{'Name'} =~ /FileName01\_([^#]+)\.csv/i) && ($item->{'Name'} !~ /FileName02/i))
     {
      $WeFoundFileName01 = 1;
      push (@FileContent::FilesOnSFTPSite,"$item->{'Name'}");
      push (@FileContent::FilesOnSFTPSiteModTime, "$item->{'LastWriteTime'}");
      print "DEBUG\:\tFILE = <$item->{'Name'}> Appears to be FileName01 for review\.\n";
     }
    if ($item->{'Name'} =~ /FileName02\_([^#]+)\.csv/i)
     {
      $WeFoundFileName02 = 1;
      push (@FileContent::FilesOnSFTPSite,"$item->{'Name'}");
      push (@FileContent::FilesOnSFTPSiteModTime, "$item->{'LastWriteTime'}");
      print "DEBUG\:\tFILE = <$item->{'Name'}> Appears to be an File for review\.\n";
#       print "DEBUG\:\tFILE = <$item->{'Name'}> Appears to be an FileName02 for review\.\n";
     }
   }
  else 
   {
    print "DEBUG\:\tFILE = <$item->{'Name'}> IS NOT either FileName01 or FileName02 for review\.\n";
    if ($item->{'Name'} =~ /FileName/i)
     {
      print "DEBUG\:\tFILE = <$item->{'Name'}> IS NOT a FileName01 or FileName02 for review\, although it does match file naming convention (escalate for further review)\.\n";
      $ProgramEmailSettings::HighEmailPriority = 1;
      push (@ProgramEmailSettings::HighEmailPriorityReason, "File $item->{'Name'} exists on SFTP Server, but does not meet file naming standards<BR>for this program. Please Review\!");
     }
   }
 }
$session->Dispose();
From the code above, this line generates no error, am assuming it successfully finds the directory I'm trying to peruse:
my $directory = 
$session->ListDirectory("${ProgramRemoteSFTPSettings::RemoteSFTPDirectory}\/");
But this line, unchanged from program on older versions of WinSCP calls via Perl:
my $items = Win32::OLE::Enum->new($directory->{'Files'});
Generates the error:
Win32::OLE(0.1713): GetOleObject() Not a Win32::OLE object at D:\Perl\PROGRAM\FilesGet.pl line X01.
And this following line: (While loop on the items returned from above error, so the previous line appears to be the issue)
while (defined($item = $items->Next))
Generates the error:
Can't call method "Next" on an undefined value at D:\Perl\PROGRAM\FilesGet.pl line X02.
Any idea as to why the setting of $directory as:

And then trying to enumerate the files in it as:
my $directory = 
$session->ListDirectory("${ProgramRemoteSFTPSettings::RemoteSFTPDirectory}\/");
Would now cause this error?
Win32::OLE(0.1713): GetOleObject() Not a Win32::OLE object at D:\Perl\PROGRAM\FilesGet.pl line 3830 (X01)?
Thanks, Joe P.

Reply with quote

Advertisement

jpantera
Joined:
Posts:
26
Location:
Los Angeles, CA

Also Fails With Simple PowerShell Script

Hello Martin / WinSCP Support,

Since I know not many are left with much Perl experience, I tried to duplicate this part of my script, using a simple PowerShell script, using the examples on the site to use the ListDirectory method, and that too, is getting an error
Error connecting to server: Exception calling "Open" with "1" argument(s): "Network error: Software caused connection abort".
Most of my scripts build a winscp.com script file on the fly, and run that way, without issue, on the old servers and new ones, but I have a few relatively important scripts that use username & password, that it is helpful to pull the list of files at the beginning of the script, using the .NET Assembly, and then assist the building of the script and file manipulation logic.

Perhaps there is something amiss with the way I registered the .COM Assembly objects?

But using the basic PowerShell example on the WinSCP web-site, without using the actual SFTP site and credentials here, this is my PowerShell script:
try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "ftp.site.com"
        UserName = "UserName"
        Password = "Password"
        SshHostKeyFingerprint = "ssh-ed25519 256 Key-value"
    }
 
    $session = New-Object WinSCP.Session
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
 
        $directory = $session.ListDirectory("/files/")
 
        foreach ($fileInfo in $directory.Files)
        {
            Write-Host ("$($fileInfo.Name) with size $($fileInfo.Length), " +
                "permissions $($fileInfo.FilePermissions) and " +
                "last modification at $($fileInfo.LastWriteTime)")
        }
    }
    finally
        {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch
{
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}
Calling this script "FileLists.PS1", it errors out at:
Error connecting to server: Exception calling "Open" with "1" argument(s): "Network error: Software caused connection abort"
I know all the credentials, server name, key are correct, they're the same values I'm using inside my winscp.com script file with the open statement that always works.

What could be amiss?

Here's the output from winscp.com /comregistration on my new server:
c:\Program Files (x86)\WinSCP>winscp.com /comregistration
WinSCP Version 6.3.6 (Build 15073 2024-11-25) (OS 10.0.20348 – Windows Server 2022 Standard)

Versions of type library {A0B93468-D98A-4845-A234-8076229AD93F}:
1.f (win32): C:\Program Files (x86)\WinSCP\WinSCPnet32.tlb (exists)
1.f (win64): C:\Program Files (x86)\WinSCP\WinSCPnet64.tlb (exists)

Classes:
Codebase file:///C:/Program Files (x86)/WinSCP/WinSCPnet.DLL (1.15.0.15073), unless stated otherwise
WinSCP.ComparisonDifferenceCollection - {28957CC8-DEBC-48D0-841B-48AD3CB3B49F} [32-bit, 64-bit]
WinSCP.FilePermissions - {90A290B2-C8CE-4900-8C42-7736F9E435C6} [32-bit, 64-bit]
WinSCP.RemoteFileInfoCollection - {39AA3D00-578C-49AF-B3E4-16CE26C710C6} [32-bit, 64-bit]
WinSCP.RemotePath - {70253534-C5DC-4EF3-9C98-65C57D79C324} [32-bit, 64-bit]
WinSCP.RemovalEventArgsCollection - {574FF430-FD40-41F9-9A04-971D3CF844B7} [32-bit, 64-bit]
WinSCP.Session - {56FFC5CE-3867-4EF0-A3B5-CFFBEB99EA35} [32-bit, 64-bit]
WinSCP.SessionOptions - {2D4EF368-EE80-4C15-AE77-D12AEAF4B00A} [32-bit, 64-bit]
WinSCP.SessionRemoteExceptionCollection - {2309282F-B89B-4F6B-AEB1-D3E1629B7033} [32-bit, 64-bit]
WinSCP.StringCollection - {E402CB1F-6219-4C79-9EDF-1914D9589909} [32-bit, 64-bit]
WinSCP.TransferEventArgsCollection - {0285917B-581A-4F6F-9A9D-1C34ABFB4E38} [32-bit, 64-bit]
WinSCP.TransferOptions - {155B841F-39D4-40C8-BA87-C79675E14CE3} [32-bit, 64-bit]
WinSCP.TransferResumeSupport - {6CED4579-0DF2-4E46-93E9-18780546B421} [32-bit, 64-bit]

Interfaces:
_SessionException - {0A84A885-2509-3B55-9D0F-66AA762FE863} (1.f) [32-bit, 64-bit]
_OperationEventArgs - {147D7B12-1149-39D0-9448-08C4E72D336F} (1.f) [32-bit, 64-bit]
_TransferResumeSupport - {1B88BA19-32A0-3E29-85DC-27143F07AEEC} (1.f) [32-bit, 64-bit]
_RemotePath - {23E83B17-029D-3621-B03B-918D889C841F} (1.f) [32-bit, 64-bit]
_RemovalOperationResult - {297A8F7E-0B4D-3EA9-906D-1C7B036E2CD5} (1.f) [32-bit, 64-bit]
_ComparisonDifference - {31383C1B-D860-3E69-A7EA-EC590C163C6D} (1.f) [32-bit, 64-bit]
_FilePermissions - {3614E47C-9752-33A0-9BB4-C134825509F1} (1.f) [32-bit, 64-bit]
_OutputDataReceivedEventArgs - {5024BEE4-3C33-3E40-B819-6EE0A59BE5DA} (1.f) [32-bit, 64-bit]
_FileTransferProgressEventArgs - {5224DACC-ADD9-37A9-AE1C-17251DCE4D92} (1.f) [32-bit, 64-bit]
_TouchEventArgs - {55222A8E-6F0A-31F0-B0F8-F282E8E8FA69} (1.f) [32-bit, 64-bit]
_RemoteFileInfo - {563945C2-24C8-305C-BCDF-00F6349A2228} (1.f) [32-bit, 64-bit]
_RemovalEventArgs - {566FC7FF-750A-34E1-8582-D7ED393A30E8} (1.f) [32-bit, 64-bit]
_TransferOptions - {5D156A3B-DC51-3BEE-B9A7-BF42E0A34B38} (1.f) [32-bit, 64-bit]
_Session - {6CA12955-D0F9-32CB-97CA-99D15E081232} (1.f) [32-bit, 64-bit]
_SessionOptions - {794290BB-6434-3FCE-AD73-C981D8CD47B6} (1.f) [32-bit, 64-bit]
_SynchronizationResult - {7FCAD223-1BB7-3D43-9B71-F623DBF76566} (1.f) [32-bit, 64-bit]
_FileOperationEventArgs - {98ACA516-8C96-32F7-94DC-B5BEF1738C5E} (1.f) [32-bit, 64-bit]
ISessionEvents - {A1334E32-4EDF-4B51-A069-DA3FF1B19A5A} (1.f) [32-bit, 64-bit]
_RemoteDirectoryInfo - {A7DF1C39-D28D-317B-B899-2902763B39BA} (1.f) [32-bit, 64-bit]
_OperationResultBase - {AF471058-7DE0-3BF9-9FF2-116909B5277D} (1.f) [32-bit, 64-bit]
_SessionLocalException - {B60C192B-4C89-3FC9-AEBF-A4FC36B398A1} (1.f) [32-bit, 64-bit]
_TransferEventArgs - {B7291577-0C5F-3C27-97A0-29B73E7FAD87} (1.f) [32-bit, 64-bit]
_TransferOperationResult - {BAB569B5-E47F-3B61-BC9F-D41BDCA884C7} (1.f) [32-bit, 64-bit]
_ChmodEventArgs - {C01E60DA-C30F-3EEB-80FF-AF3FD23F6F31} (1.f) [32-bit, 64-bit]
_QueryReceivedEventArgs - {E42BEFD0-1D1E-373D-BC83-F9309439962E} (1.f) [32-bit, 64-bit]
_FailedEventArgs - {E7C3E81A-4A2C-3441-899F-635AB43E00BE} (1.f) [32-bit, 64-bit]
_SessionRemoteException - {EACB52D9-C441-387E-A893-6F48DD6CCD98} (1.f) [32-bit, 64-bit]
_ComparisonFileInfo - {EC55C1F3-95A7-330C-8FAF-373A8B19B394} (1.f) [32-bit, 64-bit]
_CommandExecutionResult - {F258D348-714A-378B-A35E-0BB076D18274} (1.f) [32-bit, 64-bit]

Value types:
WinSCP.TransferResumeSupportState - {0ADAAEBC-4A15-4A9C-8ED4-D85F5630035C} (1.15.0.14955, 1.15.0.15073)
WinSCP.ProgressSide - {16B6D8F6-C0B4-487D-9546-A25BBF582ED6} (1.15.0.14955, 1.15.0.15073)
WinSCP.SynchronizationMode - {38649D44-B839-4F2C-A9DC-5D45EEA4B5E9} (1.15.0.14955, 1.15.0.15073)
WinSCP.SynchronizationCriteria - {3F770EC1-35F5-4A7B-A000-46A2F7A213D8} (1.15.0.14955, 1.15.0.15073)
WinSCP.TransferMode - {6B19CBFA-0D81-4B36-A587-E11AA6A06214} (1.15.0.14955, 1.15.0.15073)
WinSCP.EnumerationOptions - {6C441F60-26AA-44FC-9B93-08884768507B} (1.15.0.14955, 1.15.0.15073)
WinSCP.SshHostKeyPolicy - {8A98AB8F-30E8-4539-A3DE-A33DDC43B33C} (1.15.0.14955, 1.15.0.15073)
WinSCP.SynchronizationAction - {B1DAE3A0-5E56-4001-88D8-786F68557E28} (1.15.0.14955, 1.15.0.15073)
WinSCP.FtpMode - {D924FAB9-FCE7-47B8-9F23-5717698384D3} (1.15.0.14955, 1.15.0.15073)
WinSCP.OverwriteMode - {E0F3C3C2-C812-48F1-A711-E0BD0F703976} (1.15.0.14955, 1.15.0.15073)
WinSCP.Protocol - {F25C49A5-74A6-4E8F-AEB4-5B4E0DDF0EF9} (1.15.0.14955, 1.15.0.15073)
WinSCP.FtpSecure - {F2FC81EB-4761-4A4E-A3EC-4AFDD474C18C} (1.15.0.14955, 1.15.0.15073)

Press (U) to unregister or Esc to exit...
Thank you,
Joe P.

Reply with quote

jpantera
Joined:
Posts:
26
Location:
Los Angeles, CA

It was Firewall Rule

Please disregard this case.

All is working, Perl, although need to find a cleaner way for Win32::OLE Perl module closing when the SFTP site isn't accessible.

Found that the issue was no connectivity until a firewall rule was altered.

Reply with quote

Advertisement

You can post new topics in this forum