$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.
#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.