通过 Powershell 将多个参数传递给 cmd 行参数 - 使用什么引号以及何时使用

德尔普

在 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 中运行这些命令,那么这些就是您得到的错误。

cmd -> .\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'
比尔_斯图尔特

问题是,纬度和经度变元的“联接”直接向-GPSLatitute*-GPSLongtitude*后参数名称=标志。-初始字符阻止 PowerShell 调用变量扩展 - 请参阅GitHub 问题 #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 将运行的实际命令。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用JavaScript XMLHttpRequest将多个参数传递给@RequestBody

使用docker run命令将参数传递给Dockerfile中的CMD

将多个值传递给单个PowerShell脚本参数

Python Pandas:使用参数将多个函数传递给agg()

将数组传递给使用-File参数执行的PowerShell脚本

通过TeamCity将带有引号的参数传递给PowerShell脚本

通过PowerShell将多个查询参数传递给WebService

使用laravel将多个参数传递给onClick()函数

通过Cmd将换行符传递给PowerShell

如何使用__new__将多个参数传递给类?

通过HttpTrigger将值/参数传递给Azure Powershell函数

在PowerShell中使用变量将多个参数传递给外部程序

Powershell在参数中使用空格运行cmd

通过cygwin从Shell脚本运行时,如何将参数传递给Powershell脚本?

使用Popen将Python变量传递给Powershell参数

通过列表将参数传递给函数,并忽略未使用的参数

VBA通过函数将多个参数传递给SQL

如何通过Powershell将多值参数传递给Reporting Services报告

通过POST使用$ .ajax将多个参数传递给php函数

Docker通过传递CMD参数执行多个命令

通过AJAX将多个参数传递给PHP

将多行Powershell结果通过管道传递到一行中的多个参数中

如何使用 | 将多个参数传递给 Ruby 方法?

将 CMD 参数传递给 python

使用 Ansible 将参数列表传递给 Powershell

如何使用按钮将多个参数传递给 WPF 命令?

通过 cmd 运行带引号的 powershell 命令

使用颤振将多个参数传递给命名路由

Exchange PowerShell:如何通过变量将多个值传递给 New-DistributionGroup 命令的 -Members(或 -ManagedBy)参数