【问题标题】:Passing multiple params to a cmd line argument via Powershell - what quotes to use and when通过 Powershell 将多个参数传递给 cmd 行参数 - 使用什么引号以及何时使用
【发布时间】:2021-07-11 20:54:33
【问题描述】:

在 Powershell 中我可以运行以下命令

.\exiftool.exe '-GPSLatitude*=56.9359839838' '-GPSLongitude*=-4.4651045874' DSC01008.JPG '-overwrite_original_in_place'

这很好用,并且需要在这些参数周围放置单引号。我经历了不同展示位置的各种迭代,以上是我可以让它工作的唯一方法。

我的问题是 -> 我正在尝试用程序化值替换这些值。如下所示。

$lat = 56.9359839838
$lon = -4.4651045874
$fileName = 'DSC01008.JPG'

.\exiftool.exe -GPSLatitude*=$lat -GPSLongitude*=$lon $fileName '-overwrite_original_in_place'

我已经用单引号/反引号/双引号进行了无数次尝试,尝试连接以及我能想到的任何其他东西 - 但这种神奇的格式还没有出现!

对我做错了什么有什么想法吗?

作为一个例子,我认为这确实有效,但没有。但它与硬编码时的命令相匹配。

编辑/更新 - * 在命令中是必需的,否则它不起作用。它们用于绕过不传递参考定位器。 如果您要在 PS 中运行这些命令,则会出现这些错误。

命令-> .\exiftool.exe "-GPSLatitude*=$lat" "-GPSLongtitude*=$lon" $FileName

错误->

No Error, but the file does not get any GPS tags. Nothing actually modified.

cmd ->

$combLat = "`'-GPSLatitude*=$lat`'" # + "
$combLon = "`'-GPSLongitude*=$lon`'" # + "'"
$combined = "$combLat $combLon $fileName"
# $combined will output the following: '-GPSLatitude*=-3.4651045874' '-GPSLongitude*=55.9359839838' 'E:\CameraAddGPS\TestFolder\DSC01010.JPG' 

.\exiftool.exe $combined

错误->

.\exiftool.exe : Wildcards don't work in the directory specification
At E:\CameraAddGPS\TestFolder\demo.ps1:25 char:1
+ .\exiftool.exe $combined
+ ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Wildcards don't...y specification:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
No matching files

更新 2 - 这会起作用,但我不想更新图像两次。

 $combLat = '-GPSLatitude*=' + $lat
 $combLon = '-GPSLongitude*=' + $lon

 .\exiftool.exe $combLat $fileName '-overwrite_original_in_place'
 .\exiftool.exe $combLon $fileName '-overwrite_original_in_place'

【问题讨论】:

  • 请更新您的minimal reproducible example(图片大多不受欢迎)。
  • 您错过了分享最重要的细节,即您收到的错误消息。 -GPSLatitude* 是什么意思?我认为您需要指定不带通配符且不带=(等号)的完整选项名称,例如:-GPSLatitude $lat
  • 更新了它,并附上了一些例子。不过,您将需要 exiftool 来运行示例。命令中肯定需要通配符和 =,否则它们会失败。作为一个方面,如果我将命令分成两部分,那么我可以让它工作。 cmets 中的代码不起作用。将再次更新 Q。

标签: string powershell cmd


【解决方案1】:

问题在于纬度和经度参数直接“附加”到= 符号之后的-GPSLatitute* 和-GPSLongtitude* 参数名称。 (- 初始字符可防止 PowerShell 调用变量扩展 - 请参阅 GitHub issue #14587。)

解决此问题的一种方法是将-GPSLatitude* 和-GPSLongtitude* 参数包含在" 引号中;例如

.\exiftool.exe "-GPSLatitude*=$lat" "-GPSLongtitude*=$lon" $FileName -overwrite_original_in_place

另一种方法是在- 字符前加上反引号(`);例如:

.\exiftool.exe `-GPSLatitude*=$lat `-GPSLongitude*=$lon $fileName -overwrite_original_in_place

我更喜欢第一种变体,因为它对我来说更具可读性。

除了 #1:不需要在最后一个参数周围使用单个 ' 引号。他们不会受伤,但他们也不会做任何事情。

除了 #2:如果您在命令前加上 getargs 命令前缀,您可以看到 PowerShell 正在运行的实际命令行:

https://github.com/Bill-Stewart/getargs/releases

getargs 实用程序无需任何解析或解释即可输出实际命令行,并将显示 PowerShell 将运行的实际命令。

【讨论】:

  • 谢谢,但这不是答案。正如@mklement0 所指出的,- 很可能是问题所在。如果有帮助,我已经用更多信息更新了 Q。就像我将命令分成两部分一样,我可以让它工作。但是两次更新文件似乎很不对。
  • 关闭。你让我(再次)得到它。在您的示例中,您错过了 *.坚持下去,它就可以工作。我真正愚蠢的是我花了这么长时间尝试各种含义,我在传入时混淆了纬度/经度。所以当它更新并将其放入塞舌尔时,我认为这只是另一个参考错误 - 这是我的错误。 ..!这花了我很长时间,干杯。
  • 去编辑这个答案以添加 *,但它不会让我。把它们加进去,我会打勾以获得一个很好的答案
猜你喜欢
  • 2022-01-05
  • 1970-01-01
  • 2015-11-21
  • 1970-01-01
  • 1970-01-01
  • 2014-09-22
  • 2020-09-12
  • 2023-03-11
  • 1970-01-01
相关资源
最近更新 更多