[英]How to pass the current powershell pipe-object “$_” as a command line arg?
I'm trying to set all svn:property's on a set of piped files: 我正在尝试在一组管道文件上设置所有svn:property:
dir * -include *.cs | Select-String -simplematch -pattern "HeadURL$" | select filename | svn propset svn:keywords "HeadURL Id LastChangedBy LastChangedRevision" $_
I get following error: 我收到以下错误:
svn: Try 'svn help' for more info
svn: Explicit target required ('HeadURL Id LastChangedBy LastChangedRevision' interpreted as prop value)
The problem is, that $_ is not passed onto svn propset... 问题是,$ _没有传递给svn propset ...
What to do? 该怎么办?
For $_
to have a value you have to use it in a context where it is actually set. 要使
$_
具有值,您必须在实际设置的上下文中使用它。 For your specific scenario this means that you have to wrap your call to svn
into a ForEach-Object
cmdlet: 对于您的特定方案,这意味着您必须
ForEach-Object
svn
的调用包装到ForEach-Object
cmdlet中:
dir * -include *.cs | Select-String -simplematch -pattern "HeadURL$" | select filename | % { svn propset svn:keywords "HeadURL Id LastChangedBy LastChangedRevision" $_ }
(I have used the %
alias here for brevity) (为简洁起见,我在这里使用
%
别名)
Inside that ForEach
the variable $_
has a value and can be used. 在
ForEach
,变量$_
有一个值,可以使用。
However, I have seen some uses where the current pipeline object got appended to a program's arguments when piping into non-cmdlets. 但是,我已经看到了一些用途,当管道连接到非cmdlet时,当前管道对象被附加到程序的参数。 I haven't fully understood that so far, though.
但到目前为止,我还没有完全理解。
But to understand what you are doing here: You are trying to set svn:keywords
on every file which uses one of those. 但要了解你在这里做了什么:你试图在每个使用其中一个文件的文件上设置
svn:keywords
。 A probably more robust and readable approach would be to actually filter the list you are scanning: 一种可能更强大和可读的方法是实际过滤您正在扫描的列表:
gci * -inc *.cs |
Where-Object {
(Get-Content $_) -match 'HeadURL$'
}
(might work, haven't tested) (可能有效,尚未测试)
You can then continue by just piping that into a foreach: 然后,您可以继续将其连接到foreach中:
| ForEach-Object {
svn ps svn:keywords "HeadURL Id LastChangedBy LastChangedRevision" $_.FullName
}
Also, here you can access all properties of the file object and not need to rely on the object Select-String
gives you. 此外,在这里您可以访问文件对象的所有属性,而不需要依赖
Select-String
提供的对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.