cmd参数的Powershell变量

Pre*_*red 5 parameters powershell arguments 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
}
Run Code Online (Sandbox Code Playgroud)

例如,newcredit可以是"James",但在我的情况下,当我运行命令时-Credit将只是"$ newcredit".

问候

EBG*_*een 7

单引号('')不会扩展字符串中的变量值.您可以使用双引号("")来解决此问题:

$cmd = "& C:\exiftool\exiftool.exe -S -t -overwrite_original -Credit=$newcredit $file.FullName"
Run Code Online (Sandbox Code Playgroud)

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

$cmd = '& C:\exiftool\exiftool.exe -S -t -overwrite_original -Credit={0} {1}' -f $newcredit, $file.FullName
Run Code Online (Sandbox Code Playgroud)

如果其中任何一个参数中有一个空格,那么参数将需要在输出中用双引号括起来.在那种情况下,我肯定会使用字符串格式:

$cmd = '& C:\exiftool\exiftool.exe -S -t -overwrite_original -Credit="{0}" "{1}"' -f $newcredit, $file.FullName
Run Code Online (Sandbox Code Playgroud)