Windows 2008R2 machine. I am running multiple wsf scripts using cscript being kicked off by task scheduler. When I run the task scheduler, it aborts. When I run via cmd, it works without issue.
In these scripts, we're connecting to the same FTP site as the same user, but just pulling files of different names. the process is failing on the session.Open command in the script.
here is the code of the function that's being called that I've added some other debugs to on my side to figure out what command was failing.
Sub FTPGetWinSCP( ByVal poDebugInfo, ByVal poErrorInfo, ByVal poFTPInfo )
' Retrieves files via FTP using WinSCP; supports wildcard search; FTP or SFTP or SCP
' Dependency: Sub WriteDebug( ByVal poDebugInfo, ByVal poErrorInfo, ByVal psMessage )
' Dependency: WinSCP and WinSCP .NET Assembly ( registered dll )
' 1 ) WinSCP Client: https://winscp.net/eng/download.php
' 2 ) .dll: https://winscp.net/eng/docs/library_install
' 3 ) .dll Register: https://winscp.net/eng/docs/library_install#registering
' 4 ) The line below is required after <job> in the main .wsf that calls this subroutine
' <reference object = "WinSCP.Session" />
' Additional Notes:
' poFTPInfo.sProtocol: Sftp ( default ), Scp and Ftp
' For additional info on WinSCP capabilities: https://winscp.net/eng/docs/library_sessionoptions
' poFTPInfo.sFileNameSearch:
' * If blank then all files in the remote FTP folder will be downloaded
' * Supports wildcard * in filename searches such as "*.txt"
Call WriteDebug( poDebugInfo, poErrorInfo, 2, Now & "; Starting FTPGetWinSCP" )
Dim sErrDesc, iErrNo
Dim oSession, oSessionOptions
Dim bDeleteAfterGet, oFile, sFinalCount, sFoundRemoteFileName, oFso, i, sNewLocalFilePathName, oDirectory, oDirectoryFiles
Dim oRegExp, aRegExMatches
wscript.echo "getting from ftp"
dim winscplog, winscplogpath,winscpname, fso, scpfile
winscplog = true
winscplogpath="D:\VendorNet\Logs\"
winscpname = month(now) & day(now) & "winscp_Log.xml"
set fso = CreateObject("Scripting.FileSystemObject")
set scpfile = fso.OpenTextFile(winscplogpath & winscpname,8,True)
if winscplog then scpfile.writeline("<run> - Start function for " & WScript.ScriptName &" - " & now)
If Not poFTPInfo.bDoNotDeleteAfterGet Then bDeleteAfterGet = True
If Right( RTrim( poFTPInfo.sFTPRemoteGetFolder ), 1 ) <> "/" Then poFTPInfo.sFTPRemoteGetFolder = poFTPInfo.sFTPRemoteGetFolder & "/"
if winscplog then scpfile.writeline("begin creating winscp sessionOptions - " & now)
Set oSessionOptions = WScript.CreateObject( "WinSCP.SessionOptions" )
if winscplog then scpfile.writeline("finish creating winscp sessionOptions - " & now)
If poFTPInfo.sProtocol = "Ftp" Then oSessionOptions.Protocol = Protocol_Ftp
If poFTPInfo.sProtocol = "Scp" Then oSessionOptions.Protocol = Protocol_Scp
If poFTPInfo.bFTPPassive Then oSessionOptions.FtpMode = FtpMode_Passive
If poFTPInfo.sProtocol = "Sftp" Or poFTPInfo.sProtocol = "Scp" Then oSessionOptions.SshHostKey = poFTPInfo.sSshHostKey
With oSessionOptions
.HostName = poFTPInfo.sFTPServer
.UserName = poFTPInfo.sFTPUser
.Password = poFTPInfo.sFTPPass
.PortNumber = poFTPInfo.iFTPPort
End With
if winscplog then scpfile.writeline("finish setting properties sessionOptions, begin create session - " & now)
Set oSession = WScript.CreateObject( "WinSCP.Session" )
if winscplog then scpfile.writeline("finish creating session - " & now)
oSession.DebugLogPath = winscplogpath & "winSCP_Native.txt"
On Error Resume Next
if winscplog then scpfile.writeline("Before Open Session - " & now)
oSession.Open oSessionOptions ' failing here
if winscplog then scpfile.writeline("after open session - " & now)
iErrNo = Err.Number : sErrDesc = Err.Description
On Error GoTo 0
If iErrNo <> 0 Then
if winscplog then scpfile.writeline("after open session, error when opening session - " & now & sErrdesc)
Call WriteFile( poErrorInfo, poErrorInfo.sFilePath, poErrorInfo.sFileName, now & " - Error connecting to FTP server; WinSCP: " & iErrNo & "; " & sErrDesc )
Call SendEmail( poDebugInfo, poErrorInfo, poErrorInfo )
End If
Set oFso = CreateObject( "Scripting.FileSystemObject" )
Dim sFileSuffix, sAggregateName, sNewName, ext, iCount
sAggregateName = poFTPInfo.sFTPRemoteGetFolder
sFileSuffix = "[_DL"
If Right( sAggregateName,1 ) <> "/" Then sAggregateName = sAggregateName & "/"
If poFTPInfo.sFileNameSearch = "" Then poFTPInfo.sFileNameSearch = "*"
If Right( poFTPInfo.sLocalGetFolder,1 ) <> "\" Then poFTPInfo.sLocalGetFolder = poFTPInfo.sLocalGetFolder & "\"
sAggregateName = sAggregateName & poFTPInfo.sFileNameSearch
Call WriteDebug( poDebugInfo, poErrorInfo, 2, Now & "; Searching remote FTP for " & poFTPInfo.sFileNameSearch )
Call WriteDebug( poDebugInfo, poErrorInfo, 2, Now & "; Downloading using WinSCP from " & sAggregateName & " to " & poFTPInfo.sLocalGetFolder )
if winscplog then scpfile.writeline("before get files - " & now)
oSession.GetFiles sAggregateName, poFTPInfo.sLocalGetFolder & "*" & sFileSuffix, bDeleteAfterGet
if winscplog then scpfile.writeline("finish getting files- " & now)
Call WriteDebug( poDebugInfo, poErrorInfo, 2, Now & "; Getting files from " & poFTPInfo.sLocalGetFolder )
Set oDirectoryFiles = oFso.GetFolder( poFTPInfo.sLocalGetFolder ).Files
ext= ""
Call WriteDebug( poDebugInfo, poErrorInfo, 2, Now & "; Processing " & oDirectoryFiles.Count & " files" )
For Each oFile In oDirectoryFiles
Call WriteDebug( poDebugInfo, poErrorInfo, 2, Now & "; Name is " & Right( oFile.Name,4 ) & " t/f: " & ( Right( oFile.Name,4 ) = sFileSuffix ) )
If Right( oFile.Name,4 ) = sFileSuffix Then
sNewName = Replace(oFile.Name,sFileSuffix,"")
Call WriteDebug( poDebugInfo, poErrorInfo, 2, Now & "; sNewName is " & sNewName )
' make sure the file does not already exist in the local directory when downloading, append with .1, .2, etc.
iCount = 1
Do While oFso.FileExists( poFTPInfo.sLocalGetFolder & sNewName & ext)
ext= "." & cstr(iCount)
iCount = iCount + 1
Loop
sNewName = sNewName & ext
Call WriteDebug( poDebugInfo, poErrorInfo, 2, Now & "; Setting name to " & sNewName )
oFile.Name = sNewName
End If
Next
if winscplog then scpfile.writeline("before disposing session - " & now)
oSession.Dispose
if winscplog then scpfile.writeline("after dispose session - " & now)
if winscplog then scpfile.writeline("</run> - End function for " & WScript.ScriptName & " - " & now)
scpfile.close
set fso = nothing
Set aRegExMatches = Nothing
Set oRegExp = Nothing
Set oFso = Nothing
Set oDirectory = Nothing
Set oDirectoryFiles = Nothing
Set oSession = Nothing
Set oSessionOptions = Nothing
Call WriteDebug( poDebugInfo, poErrorInfo, 2, Now & "; Finished FTPGetWinSCP" )
End Sub
Each .wsf script running connects around 4-5 times to the same site pulling different files. There are 5 of these all running, which can create 20+ connections to the same FTP site in close proximity to one another.
I believe by running these scripts simultaneously I can reproduce this fairly close to on-demand. I have this installed in a test environment right now, please let me know if there's any more debug I can provide. I've attached the debug from a failed run with session.debuglogpath set.