简体   繁体   English

PowerShell 中的管道:不将参数绑定到参数<parameter>因为它是 null</parameter>

[英]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-ObjectSort-ObjectGroup-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.

相关问题 PowerShell-复制项目无法将参数绑定到参数“路径”,因为它是 null - PowerShell- copy item cannot bind argument to parameter 'path' because it is null 错误 - 无法将参数绑定到参数“InputObject”,因为它为空 - Error - Cannot bind argument to parameter 'InputObject' because it is null 使用答案时Powershell“无法将参数绑定到参数”错误 - Powershell 'cannot bind argument to parameter' error when using answer 无法通过注册表正确调用 Powershell 脚本。 找不到接受参数“$null”的位置参数 - Can't call a Powershell script through the registry properly. A positional parameter cannot be found that accepts argument '$null' 服务器上的远程Powershell表示参数的参数为​​空字符串 - Remote powershell on server says argument for parameter is empty string 参数与参数不兼容? - Argument incompatible with parameter? Powershell参数错误&#39;p&#39; - Powershell parameter error 'p' Powershell Remove-WmiObject:指定的参数超出了有效值的范围。 参数名称:路径 - Powershell Remove-WmiObject: Specified argument was out of the range of valid values. Parameter name: path PowerShell 将文字路径设置为参数路径 - PowerShell set literalpath as parameter path 在 Powershell 中禁用位置参数绑定 - Disable Positional Parameter Binding in Powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM