cmd参数的Powershell变量

cmd参数的Powershell变量,powershell,parameters,cmd,arguments,Powershell,Parameters,Cmd,Arguments,我想对cmd参数使用powershell变量,但我不知道如何使用它 function iptc($file) { $newcredit = correspondance($credit) $cmd = '& C:\exiftool\exiftool.exe -S -t -overwrite_original -Credit=$newcredit $file.FullName' Invoke-Expression $cmd }

我想对cmd参数使用powershell变量,但我不知道如何使用它

function iptc($file)
{        
        $newcredit = correspondance($credit)
        $cmd = '& C:\exiftool\exiftool.exe -S -t -overwrite_original -Credit=$newcredit $file.FullName'
        Invoke-Expression $cmd
}
例如,newcredit可以是“James”,但在我的例子中,当我运行命令时,Credit将仅为“$newcredit”

单引号(“”)不展开字符串中的变量值。您可以使用双引号(“”)来解决此问题:

或者,通过我最常用的方法,通过使用字符串格式:

$cmd = '& C:\exiftool\exiftool.exe -S -t -overwrite_original -Credit={0} {1}' -f $newcredit, $file.FullName
$cmd = '& C:\exiftool\exiftool.exe -S -t -overwrite_original -Credit="{0}" "{1}"' -f $newcredit, $file.FullName
如果其中任何一个参数中有空格,则该参数需要在输出中用双引号括起来。在这种情况下,我肯定会使用字符串格式:

$cmd = '& C:\exiftool\exiftool.exe -S -t -overwrite_original -Credit={0} {1}' -f $newcredit, $file.FullName
$cmd = '& C:\exiftool\exiftool.exe -S -t -overwrite_original -Credit="{0}" "{1}"' -f $newcredit, $file.FullName

单引号(“”)不展开变量值。请改用双引号(“”)或使用字符串格式(-format)。非常感谢,现在可以使用了!非常感谢。我还有一个问题,当$newcredit由两个单词组成时(如“James Bond”),-Credit只接受“James”,请问我能为此做些什么?谢谢,它可以工作,但我必须在新Credit的双引号中加一个反勾:
-Credit=“{0}
”否则它就不起作用了,现在如果我有两个空格,它就不起作用了,或者如果没有任何空格,它就会像
“James
”`,奇怪。据我所知,这太难了:如果必须在字符串内部转义双引号,那么必须在字符串外部使用双引号。如果参数中的双引号导致exiftool阻塞,则不能为该程序提供带空格的参数。您是否完全运行了我发布的行?是的,我完全运行了您发布的行,但我有一个错误:
+CategoryInfo:ParserError:(:String)[Invoke Expression],uncompleteParseException+FullyQualifiedErrorId:TerminatorExpectedAtonForString,Microsoft.PowerShell.Commands.InvokeeExpressionCommand
在脚本中添加一行,将主机$cmd写入控制台。复制并粘贴它吐出的内容。这个命令会执行吗?