tomohulk / WinSCP

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

using psdrive gives a Cannot bind argument to parameter 'Path' because it is null. #147

Closed madsholme closed 1 year ago

madsholme commented 1 year ago

Issue Description

When using psdrive as a path to download/upload files it failes with "cannot create..." if i run the same script and do a dir MyPSDrive:\ it output the files correctly.

Example

$GetFilesFacility= "\serverapp\d$\Moved_files\MyFiles"

$User = "ScriptUser"
$PWord = ConvertTo-SecureString -String "MyPassword" -AsPlainText -Force

$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord

new-psdrive  -name "GetFilesFacility" -root $GetFilesFacility -PSProvider "FileSystem" -Credential $Credentials | out-null

    write-host "Loader winscpnet.dll" -f green
    # Load WinSCP .NET assembly
    Add-Type -Path "C:\Task\WinSCPnet.dll"

    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        SshHostKeyFingerprint = "ssh-rsa 2048dsadsadas:0b:dd:c5:41:dsadas"
        HostName = "mysite.com"
        UserName = "user"
        Password = "4pass"

write-host "Forbinder til ftp" -f green
        $session.Open($sessionOptions)

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

        $transferResult =  
            $session.GetFiles("/export/*", "GetFilesFacility:\", $False, $transferOptions)

        # Throw on any error
        $transferResult.Check()

        # Print results
        foreach ($transfer in $transferResult.Transfers)
        {
            Write-Host "Download of $($transfer.FileName) succeeded"
        }

Expected Output

Error: Exception calling "Check" with "0" argument(s): "Can't create file 'GetFilesFacility:\2023-03-21.txt'.
System Error.  Code: 123.
The filename, directory name, or volume label syntax is incorrect"

[ERROR  ]  Execution finished with the following error (winrm-exec.py:325)[root]
[ERROR  ]  C:\WINDOWS\TEMP\741-906-admdc01-dispatch-script.tmp.ps1 : System.Management.Automation.ParameterBindingValidationExcept
ion: Cannot bind argument to parameter 'Path' because it is null.
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception ex
ception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
At line:1 char:1
+ C:\WINDOWS\TEMP\741-906-admdc01-dispatch-script.tmp.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,741-906-admdc01-dispatch-script.tmp.ps 
   1

WinSCP-PowerShell Version

5.21.7.0

Environment

Windows server 2016

madsholme commented 1 year ago

just a note, this is from a remote execusion with winrm, but the same thing happens when i run it local.

tomohulk commented 1 year ago

Is this your exact code? you are missing a \ in your path $GetFilesFacility= "\serverapp\d$\Moved_files\MyFiles" should be $GetFilesFacility= "\\serverapp\d$\Moved_files\MyFiles"

Also, you are using the GetFiles method on the $session object. Thats calling the winscp.dll directly. Clone this repo with the 5.21.8 assembly and use the Receive-WinSCPItem cmdlet over that direct method call. I do some path validation and resolving in the wrapper function.

tomohulk commented 1 year ago

So, I had a little bit of time, I replicated this all out, and Receive-WinSCPItem works with a PSDrive. Im closing this out.

image