$session.FileExists throws an error

Advertisement

robbay828
Guest

$session.FileExists throws an error

I have the following script in Powershell:

#variables for filename
$strDate = Get-Date -format "yyyyMMdd"
$strFileName = "ug_expected_" + $strDate + ".txt"
$n = 1

#settings forftp to Slate and grab filename


$localToFTPPath = "$strFileName"
$localFromFTPPath = "$strFileName"
$remotePickupDir = "/outgoing/"
$remoteDropDir = "/incoming/"
$fullFilePathName = "/outgoing/$strFileName"

#email message settings

$From = "xxxxxx@XXXXX.edu"
$To = "xxxxxx@XXXXX.edu"
$Subject = "SlateFileGetUG.ps1 Error"
$SMTPServer = "XXXXX.XXXXXX.edu"
$SMTPPort = "25"

Add-Type -Path "WinSCPnet.dll"


#Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName ="XXXXXXXXXXXXXXXXXXXXXXX"
UserName ="XXXXXXXXXXXXXXXXXXXXXXXX"
Password ="XXXXXXXXXXXXXXXXXXXXXXXX"
sshHostKeyFingerprint = "ssh-rsa 2048 35:7a:60:48:cf:4f:bc:d1:f1:4e:92:dc:ea:02:18:3b"
}

$session = New-Object WinSCP.Session


#Connect
$session.Open($sessionOptions)

# Download files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Ascii

try
{
if ($session.FileExists($fullFilePathName))
{
Write-Host "File $fullFilePathname exists"
$transferResult = $session.GetFiles($fullFilePathName, "c:\slate\", $False, $transferOptions)
$transferResult.Check()
$session.Dispose()
exit 0
}
else
{
Write-Host "File $fullFilePathName does not exist"
$session.Dispose()
exit 1
}
}
catch
{

$session.Dispose()
Write-Host "Error: $($_.Exception.Message)"

#Email message
$Body = "Import process encountered an exeception! " + $($_.Exception.Message)`

Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort

exit 2
}


The if then statement doesn't work. I get an error message and the script falls into the catch section of the code.

The error message is: Exception claling "FileExists" with "1" argument(s): "'.', hexadecimal value 0x00, is an invalid character. Line 18, position 28."

This only happens when the file does not exist on the remote server.

Kinda makes the if/then void of purpose.

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
29,502
Location:
Prague, Czechia

Re: $session.FileExists throws an error

Please attach a full session log and debug log files showing the problem (using the latest version of WinSCP).

To generate the log files, set Session.SessionLogPath and Session.DebugLogPath. Submit the log with your post as an attachment. If you do not want to post the log publicly, you can mark the attachment as private.

Reply with quote

robbay828
Guest

Re: $session.FileExists throws an error

The script did not crash as it does without the log entries but I still see the errors in the logs. I guess I'm wondering if I am checking for the existence of the file correctly?

Thank you for reviewing the log files and providing any insight you might have.

martin wrote:

Please attach a full session log and debug log files showing the problem (using the latest version of WinSCP).

To generate the log files, set Session.SessionLogPath and Session.DebugLogPath. Submit the log with your post as an attachment. If you do not want to post the log publicly, you can mark the attachment as private.
  • DebugLogPath.txt (36.54 KB, Private file)
  • SessionLogPath.txt (9.7 KB, Private file)

Reply with quote

Advertisement

You can post new topics in this forum