Question about using PowerShell Select-String, exiftool(-k)

强颜欢笑 提交于 2020-03-15 05:48:30

问题


In a PowerShell script I'm trying to filter the output of the exiftool(-k).exe command below, using Select-String.

I've tried numerous permutations, but none work, and I always see the unfiltered output. What do I need to do to filter the output of this command?

Start-Process -FilePath "C:\PowerShell\exiftool(-k).exe" -ArgumentList test.jpg |
  Select-String -pattern 'GPS' -SimpleMatch

回答1:


The naming is inconvenient. Rename it to exiftool.exe and run it without start-process.

rename-item 'exiftool(-k).exe' exiftool.exe    

C:\Users\bfbarton\PowerShell\exiftool.exe test-jpg | Select-String GPS 

Or

$env:path += ';C:\Users\bfbarton\PowerShell'
exiftool test.jpg | Select-String GPS

The website recommends to 'rename to "exiftool.exe" for command-line use'. https://exiftool.org . Even in unix, it wouldn't work without escaping the parentheses.

There's also the option of using the call operator. Using tab completion actually does this:

& '.\exiftool(-k).exe' test.jpg | select-string gps



回答2:


  • You cannot directly receive output from a Start-Process call[1], so using it in a pipeline is pointless.

    • In fact, on Windows your program launched with Start-Process runs in a different, new window, which is where you saw the unfiltered output (given that no Select-String was applied there); in your calling window, Start-Process produced no output at all, and therefore nothing was sent to Select-String, and the pipeline as a whole produced no output.
  • Never use Start-Process to synchronously invoke a console application whose output you want to capture or redirect - simply call the application directly:

& "C:\PowerShell\exiftool(-k).exe" test.jpg | Select-String GPS -SimpleMatch

Note that &, the call operator, is needed for this invocation, because your executable path is (double-)quoted (of necessity here, because the file name contains ( and )); & is only needed for executable paths that are quoted and/or contain variable references; you wouldn't need it to call git ..., for instance.


[1] While you would see the program's output in the caller's window if you added -NoNewWindow -Wait to a Start-Process call, you still wouldn't be able to capture, pass on or redirect it.



来源:https://stackoverflow.com/questions/60306323/question-about-using-powershell-select-string-exiftool-k

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!