How to pass the current powershell pipe object "$ _" as an arg command line?

I am trying to set all svn: property in a set of files with channels:

dir * -include *.cs | Select-String -simplematch -pattern "HeadURL$" | select filename | svn propset svn:keywords "HeadURL Id LastChangedBy LastChangedRevision" $_ 

I get the 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 being transmitted to svn propset ...

What to do?

+4
source share
1 answer

For $_ , to make a difference, you must use it in the context where it is actually installed. For your specific scenario, this means that you need to pass your svn call to the ForEach-Object :

 dir * -include *.cs | Select-String -simplematch -pattern "HeadURL$" | select filename | % { svn propset svn:keywords "HeadURL Id LastChangedBy LastChangedRevision" $_ } 

(I used the alias % for short)

Inside ForEach variable $_ is relevant and can be used.

However, I saw some uses when the current pipeline object was added to the program arguments when connecting to non-cmdlets. I have not figured this out yet.

But to understand what you are doing here: you are trying to install svn:keywords on each file that uses one of them. Probably a more robust and readable approach would be to actually filter the list you are viewing:

 gci * -inc *.cs | Where-Object { (Get-Content $_) -match 'HeadURL$' } 

(may work, not tested)

Then you can continue by simply laying this in foreach:

 | ForEach-Object { svn ps svn:keywords "HeadURL Id LastChangedBy LastChangedRevision" $_.FullName } 

In addition, here you can access all the properties of the file object and you do not need to rely on the Select-String object.

+9
source

All Articles