tomohulk / WinSCP

WinSCP PowerShell Wrapper Module
GNU General Public License v3.0
153 stars 30 forks source link

Unable to get the error back #119

Closed ltiwana closed 4 years ago

ltiwana commented 4 years ago

Please fill in the following fields, replacing the values inside the brackets {{ }}.

Issue Description

{{ Please provide a description here. }} I am not able to do any error check, in case any of the module commands fail. Nothing shows up under $?, $lastexitcode or Try { } Catch { }

Example

{{ Please provide an example of the code being used. }}


Test-Path : Access is denied
At C:\Program Files\WindowsPowerShell\Modules\WinSCP\5.15.3.0\Public\Receive-WinSCPItem.ps1:59 char:17
+             if (Test-Path -Path $LocalPath) {
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (\\cambie-fs01\a...a enVista Files:String) [Test-Path], UnauthorizedAccessException
    + FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.TestPathCommand

Receive-WinSCPItem : WinSCP.SessionRemoteException: Can't create file '\\DevServer\files\Excel 44.xlsx.filepart'.
System Error.  Code: 5.
Access is denied
At line:1 char:6
+ try {Receive-WinSCPItem -WinSCPSession $Session -RemotePath $DownDirS ...
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Receive-WinSCPItem

Expected Output

{{ Please provide what expect to happen. }} $? should be False, and try { } catch { } should be able to catch if command-let returns an error

Actual Output

{{ Please provide what actually happened. }}

 $? is True
$lastexitCode is empty
Try { } catch { } is not able capture the error.

WinSCP-PowerShell Version

{{ Paste output of Get-Module -Name WinSCP | Clip here }}\

ModuleType Version    Name                                ExportedCommands                                                                                                                          
---------- -------    ----                                ----------------                                                                                                                          
Script     5.15.3.0   WinSCP                              {ConvertTo-WinSCPEscapedString, Copy-WinSCPItem, Get-WinSCPChildItem, Get-WinSCPHostKeyFingerprint...}                                    

Environment

{{ Please provide OS Information (Version, Arch, etc), PowerShell Version (Output of $host) and what FTP flavor the server is. }}

Name             : Windows PowerShell ISE Host
Version          : 5.1.14393.3053

OS Name:                   Microsoft Windows Server 2016 Standard
OS Version:                10.0.14393 N/A Build 14393
tomohulk commented 4 years ago

interesting, I am using write-error so try/catch should work. Give some time and ill trying and get this sorted out. Thanks for letting me know.

jeffreymcclain commented 4 years ago

@ltiwana See if this format works:

try {
    Receive-WinSCPItem -WinSCPSession $Session -RemotePath $DownDirS ... -ErrorAction Stop
} catch [System.Exception] {
    $Error | Out-String
}

Make sure to include the "-ErrorAction Stop" and "[System.Exception]" parts.

tomohulk commented 4 years ago

im going to close this. I am able to catch errors with Try, Catch. and its been quite a while since the authors last response.