[英]Piping in PowerShell: Connot bind argument to parameter <parameter> because it is null
I have been practicing PowerShell by dealing with some of the tasks I could do in the file explorer.通过处理我可以在文件资源管理器中完成的一些任务,我一直在练习 PowerShell。 I am organizing some files for a python project which I am doing.我正在为我正在做的 python 项目组织一些文件。 My goal was to copy all python files in the current directory into the "V0.0_noProgressBar" directory:我的目标是将当前目录中的所有 python 文件复制到“V0.0_noProgressBar”目录中:
ls -Filter "*.py" | copy $_ "V0.0_noProgressBar"
but it fails:但它失败了:
Cannot bind argument to parameter 'Path' because it is null.
At line:1 char:26
+ ls -filter "*.py" | copy $_ "V0.0_noProgressBar"
+ ~~
+ CategoryInfo : InvalidData: (:) [Copy-Item], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,
Microsoft.PowerShell.Commands.CopyItemCommand
I assume this should be sufficient information to figure this out, let me know if more is needed.我认为这应该是足够的信息来解决这个问题,如果需要更多信息,请告诉我。 I have run into similar issues a number of times, so there must be a fundamental problem with my understanding of the placeholder $_
.我已经多次遇到类似的问题,所以我对占位符$_
的理解肯定存在根本问题。
This can be simplified to:这可以简化为:
Copy-Item *.py V0.0_noProgressBar
To answer original question, why $_
is not working:要回答原始问题,为什么$_
不起作用:
$_
is only valid in script contexts, not just anywhere in the pipeline. $_
仅在脚本上下文中有效,而不仅仅在管道中的任何地方有效。 E. g.例如。 you could use it in a script block of ForEach-Object
:您可以在ForEach-Object
的脚本块中使用它:
Get-ChildItem -filter "*.py" | ForEach-Object { Copy-Item $_ "V0.0_noProgressBar" }
To augment zett42's succinct answer .增加zett42 的简洁答案。 $_
makes an appearance in several areas of PowerShell. $_
出现在 PowerShell 的几个区域。 Some cmdlets allow it's use in a script block the output of which is treated as the argument to the parameter.一些 cmdlet 允许它在 output 的脚本块中使用,其中 Z78E6221F6393D14CE6DZ 被视为参数的参数。 In keeping with the question the *-Item
cmdlets can make use of $_
.根据问题, *-Item
cmdlet 可以使用$_
。
Get-ChildItem -Filter "*.txt" | Copy-Item -Destination { "C:\"+ $_.Name }
Obviously that's just an example.显然这只是一个例子。 Perhaps a more useful case is Rename-Item -NewName {... $_... }
.也许更有用的情况是Rename-Item -NewName {... $_... }
。 -NewName
which also works this way. -NewName
也以这种方式工作。
Other common cmdlets that make use of $_
are Select-Object
, Sort-Object
, & Group-Object
.其他使用$_
的常见 cmdlet 是Select-Object
、 Sort-Object
和Group-Object
。 Overlapping some of these $_
is used by many cmdlets to help define calculated properties .许多 cmdlet 使用其中一些重叠的$_
来帮助定义计算的属性。 I strongly recommend reading this about topic.我强烈推荐阅读这篇关于主题的文章。 Along with the use of $_
calculated properties are extremely useful.与使用$_
一起计算的属性非常有用。 I use them with Select-Object
everyday!我每天都将它们与Select-Object
一起使用!
If you really want to pass a collection of files through a pipeline, you can do this:如果你真的想通过管道传递一组文件,你可以这样做:
ls -Filter *.py | Copy-Item -Destination "V0.0-NoProgressBar"
Here the Copy-Item cmdlet has no -Path parameter, so it gets the files from the pipeline.这里的 Copy-Item cmdlet 没有 -Path 参数,因此它从管道中获取文件。 See Help Copy-Item -full to see that the -Path parameter can accept pipeline input.请参阅帮助 Copy-Item -full 以查看 -Path 参数是否可以接受管道输入。
This is not as simple as answers already given, but it does show an alternative to trying to use $_ in a context where it is unavailable.这并不像已经给出的答案那么简单,但它确实显示了在 $_ 不可用的情况下尝试使用它的替代方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.