You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Parameter binding error in Copy-Item -ToSession when using range wildcards in -Path #6527
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-CommandCopy-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-Helpcopy-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.
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 : {}
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
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!
Reacted by Rain Sallow (/u/ta11ow), Willy Moselhy [MSFT], Marc Selis and Aftiii
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?
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.
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.
Activity
BrucePay commentedon Mar 29, 2018
Vladimir (@dronkoff) What does
Get-Command Copy-Itemreturn? And can you show the detailed error output i.e. after running your command, doThanks!
dronkoff commentedon Mar 30, 2018
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.
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.
Here are exception details.
BrucePay commentedon Mar 30, 2018
Vladimir (@dronkoff) Currently I can't repro this on either 5.1 or 6.0. If I specify
$nullfor the session as you suggest, I getwhich is the expected error.
I wanted you to run
Get-Commandto see where the cmdlet was coming from in the (admittedly unlikely) case that you somehow were picking up a command alias:dronkoff commentedon Apr 3, 2018
Ah, I got the idea. No, it's the original one.
Maybe locale related issues? Because with $null I have another result.
dhjensen commentedon Aug 22, 2018
It's the same issue with -FromSession
using wildcard(*) give the exception with that as well.
BarnumD commentedon Nov 6, 2018
Same issue on powershell core 6.1 when copying a file with a wildcard.
catebest commentedon Dec 15, 2018
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!
RoboMWM commentedon Feb 19, 2020
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?
vexx32 commentedon Feb 19, 2020
RoboMWM you can either use
-LiteralPathor escape the brackets with `AspenForester commentedon Feb 24, 2021
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
-Pathparameter to a valid path that actually contains a non-zero number of items to copy, the command completes successfully.SteveL-MSFT commentedon Jun 4, 2021
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.