Skip to content

Parameter binding error in Copy-Item -ToSession when using range wildcards in -Path #6527

Closed

Description

@dronkoff

Steps to reproduce

$sss = New-PSSession server
Copy-Item -Path 'c:\temp\[x]*' -Destination 'c:\temp' -ToSession $sss

Expected behavior

Everything that starts with the letter 'x' in the temp folder was copied to server "server".

Actual behavior

PS C:\> Copy-Item -Path 'c:\temp\[x]*' -Destination 'c:\temp' -ToSession $sss
Copy-Item : A parameter cannot be found that matches parameter name 'ToSession'.
At line:1 char:55
+ Copy-Item -Path 'c:\temp\[x]*' -Destination 'c:\temp' -ToSession $sss
+                                                       ~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Copy-Item], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand

Environment data

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.16299.251
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.16299.251
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Activity

  1. BrucePay commented on Mar 29, 2018

    @BrucePay
    Collaborator

    Vladimir (@dronkoff) What does Get-Command Copy-Item return? And can you show the detailed error output i.e. after running your command, do

    $error[0] | fl * -force
    

    Thanks!

  2. dronkoff commented on Mar 30, 2018

    @dronkoff
    Author

    Bruce Payette (@BrucePay) If you mean if this parameter exists? Yes, it is. And it even works as expected if I wouldn't specify square brackets in Path param.

    PS C:\> Get-Command Copy-Item -syntax
    
    Copy-Item [-Path] <string[]> [[-Destination] <string>] [-Container] [-Force] [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-Recurse] [-PassThru] [-Credential <pscredential>] [-WhatIf] [-Confirm] [-UseTransaction] [-FromSession <PSSession>] **[-ToSession <PSSession>]** [<CommonParameters>]
    
    Copy-Item [[-Destination] <string>] -LiteralPath <string[]> [-Container] [-Force] [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-Recurse] [-PassThru] [-Credential <pscredential>] [-WhatIf] [-Confirm] [-UseTransaction] [-FromSession <PSSession>] **[-ToSession <PSSession>]** [<CommonParameters>]
    
    PS C:\> Get-Help copy-item -Parameter ToSession
    
    -ToSession <PSSession>
        Specifies the PSSession object to which a remote file is being copied. When you use this par
        ameter, the Path and LiteralPath parameters refer to the local path on the remote machine.
    
        Required?                    false
        Position?                    named
        Default value                None
        Accept pipeline input?       False
        Accept wildcard characters?  false

    You can also try to reproduce it on your machine. You don't even need a real session and folders for a simple test. From my observations it can be easily reproduced on every installation of Windows PowerShell 5.0 and 5.1. Also I reproduced it on one installation of PowerShell Core 6.0.

    Copy-Item -Path 'c:\temp\[x]*' -Destination 'c:\temp' -ToSession $null

    Here are exception details.

    PS C:\> $Error[0] | fl * -Force
    
    
    PSMessageDetails      :
    Exception             : System.Management.Automation.ParameterBindingException: A parameter cannot be found that matches parameter name 'ToSessio
                            n'.
                               at System.Management.Automation.CmdletParameterBinderController.VerifyArgumentsProcessed(ParameterBindingException ori
                            ginalBindingException)
                               at System.Management.Automation.CmdletParameterBinderController.BindCommandLineParametersNoValidation(Collection`1 arg
                            uments)
                               at System.Management.Automation.CmdletParameterBinderController.BindCommandLineParameters(Collection`1 arguments)
                               at System.Management.Automation.CommandProcessor.BindCommandLineParameters()
                               at System.Management.Automation.CommandProcessor.Prepare(IDictionary psDefaultParameterValues)
                               at System.Management.Automation.CommandProcessorBase.DoPrepare(IDictionary psDefaultParameterValues)
                               at System.Management.Automation.Internal.PipelineProcessor.Start(Boolean incomingStream)
                               at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)
                            --- End of stack trace from previous location where exception was thrown ---
                               at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                               at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)
                               at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal
                            [][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections, FunctionContext funcCont
                            ext)
                               at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
                               at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
    TargetObject          :
    CategoryInfo          : InvalidArgument: (:) [Copy-Item], ParameterBindingException
    FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
    ErrorDetails          :
    InvocationInfo        : System.Management.Automation.InvocationInfo
    ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
    PipelineIterationInfo : {}
  3. BrucePay commented on Mar 30, 2018

    @BrucePay
    Collaborator

    Vladimir (@dronkoff) Currently I can't repro this on either 5.1 or 6.0. If I specify $null for the session as you suggest, I get

    PS[1] (86) > Copy-Item -Path 'c:\temp\[x]*' -Destination 'c:\temp' -ToSession $null
    Copy-Item : Cannot validate argument on parameter 'ToSession'. The argument is null or empty. ...
    

    which is the expected error.

    I wanted you to run Get-Command to see where the cmdlet was coming from in the (admittedly unlikely) case that you somehow were picking up a command alias:

    PS[1] (89) > gcm copy-item
    
    CommandType     Name                                               Version    Source
    -----------     ----                                               -------    ------
    Cmdlet          Copy-Item                                          3.1.0.0    Microsoft.PowerShell.Management
    
  4. dronkoff commented on Apr 3, 2018

    @dronkoff
    Author

    Ah, I got the idea. No, it's the original one.

    PS C:\> Get-Command Copy-Item
    
    CommandType     Name                                               Version    Source
    -----------     ----                                               -------    ------
    Cmdlet          Copy-Item                                          3.1.0.0    Microsoft.PowerShell.Management

    Maybe locale related issues? Because with $null I have another result.

    PS C:\> Copy-Item -Path 'c:\temp\[x]*' -Destination 'c:\temp' -ToSession $null
    Copy-Item : A parameter cannot be found that matches parameter name 'ToSession'.
    At line:1 char:55
    + ... opy-Item -Path 'c:\temp\[x]*' -Destination 'c:\temp' -ToSession $null
    +                                                          ~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Copy-Item], ParameterBindingException
        + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
  5. dhjensen commented on Aug 22, 2018

    @dhjensen

    It's the same issue with -FromSession
    using wildcard(*) give the exception with that as well.

  6. BarnumD commented on Nov 6, 2018

    @BarnumD

    Same issue on powershell core 6.1 when copying a file with a wildcard.

  7. catebest commented on Dec 15, 2018

    @catebest

    I encountered this issue on PowerShell 5.1.14393.2636 with Copy-Item version 3.1.0.0 when my Path parameter value was invalid. A more appropriate error message would be a great fix!

  8. RoboMWM commented on Feb 19, 2020

    @RoboMWM

    Also having this issue when trying to copy a file that's in a folder with square brackets in its name. (Not using a wildcard in my case.) Is there any way to "escape" these characters?

  9. vexx32 commented on Feb 19, 2020

    @vexx32
    Collaborator

    RoboMWM you can either use -LiteralPath or escape the brackets with `

  10. AspenForester commented on Feb 24, 2021

    @AspenForester

    With PowerShell 7.1.1, I'm able to reproduce the error when I enter a valid but empty source path, but if I point the -Path parameter to a valid path that actually contains a non-zero number of items to copy, the command completes successfully.

  11. SteveL-MSFT commented on Jun 4, 2021

    @SteveL-MSFT
    Member

    I looked into this. What's happening is that the parameter binder is trying to find the right provider for dynamic parameters. It correctly identifies the FileSystemProvider based on the path, but then it calls the provider to glob the path. No result is found since that path doesn't match anything. There is a check for result to be found and doesn't attempt to bind dynamic parameters if nothing is returned. This results in the parameter binder error.

  12. added
    Issue-Bug
    Resolution-Fixed
    on Jun 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-BugResolution-FixedWG-Cmdlets

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions