Powershell: Parameter binding error in Copy-Item -ToSession when using range wildcards in -Path

Created on 29 Mar 2018  路  9Comments  路  Source: PowerShell/PowerShell

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

Area-Cmdlets-Management

Most helpful comment

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!

All 9 comments

@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!

@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 : {}

@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

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

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

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

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!

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?

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

Was this page helpful?
0 / 5 - 0 ratings