tomohulk / WinSCP

WinSCP PowerShell Wrapper Module
GNU General Public License v3.0
151 stars 29 forks source link

Trying to wrap Move/Rename-WinSCPItem in an if statement #126

Closed jimjimbojimbob closed 4 years ago

jimjimbojimbob commented 4 years ago

Issue Description

Question: Trying to wrap Move-WinSCPItem and Rename-WinSCPItem in a if statement in order to test for success and handle errors doesn't quite work how I expect. The file is renamed successfully but the output of the if statement is false even though confusingly $? reports as True.

This may be my misunderstanding as fairly new to Powershell

Example

if (Move-WinSCPItem -WinSCPSession $Session -Path test1.txt -Destination test2.txt) { write-host "move was successful" } else { write-host "move failed" } move failed

$? True

Expected Output

if (Move-WinSCPItem -WinSCPSession $Session -Path test1.txt -Destination test2.txt) { write-host "move was successful" } else { write-host "move failed" } move was successful

$? True

Actual Output

Shows that file test1.txt exists on the server

Get-WinSCPChildItem -WinSCPSession $Session

Mode LastWriteTime Length Name 13/04/2020 14:54:17 5 test1.txt

Attempt to rename to a filename that does not already exist. Reports as failed but does actually complete successfully

if (Move-WinSCPItem -WinSCPSession $Session -Path test1.txt -Destination test2.txt) { write-host "move was successful" } else { write-host "move failed" } move failed

$? True

Shows that the command did in fact work and that file test1.txt has been renamed to test2.txt

Get-WinSCPChildItem -WinSCPSession $Session

Mode LastWriteTime Length Name 13/04/2020 14:54:17 5 test2.txt

WinSCP-PowerShell Version

ModuleType Version Name ExportedCommands

Script 5.17.2.0 WinSCP {ConvertTo-WinSCPEscapedString, Copy-WinSCPItem, Get-WinSCPChildItem, Get-WinSCPHostKeyFingerprint...}

Environment

Windows 10 Pro Version 10.0.18363 Build 18363

$host Name : ConsoleHost Version : 5.1.18362.628 InstanceId : 6ab09edb-8d95-4a6b-98a3-7ac554d4defc UI : System.Management.Automation.Internal.Host.InternalHostUserInterface CurrentCulture : en-GB CurrentUICulture : en-GB PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy DebuggerEnabled : True IsRunspacePushed : False Runspace : System.Management.Automation.Runspaces.LocalRunspace

FileZillaServer version 0.9.6 (apologies this is all I have available to me to test with)

tomohulk commented 4 years ago

as this is just a wrapper around the .net assembly, I have tried to keep it at that. So based on that, the MoveFile method on the WinSCP.Session object doesn't return anything. With that being said, I would use the Test-WinSCPPath cmdlet and test if the rename was successful.

Move-WinSCPItem -WinSCPSession $Session -Path test1.txt -Destination test2.txt
if (Test-WinSCPPath -WinSCPSession $Session -Path test2.txt) {
    Write-Output -InputObject "Move Successful."
} else {
    Write-Output -InputObject "Move Failed!"
}

Hope that helps.

jimjimbojimbob commented 4 years ago

That's what I've done, thanks for the explanation. I'll close the issue.

Thanks,

Jim