在Powershell脚本中的参数。

huangapple go评论54阅读模式
英文:

Arguments in Powershell Script

问题

我尝试使用ImageMagick处理图像。在命令行中,我使用以下命令成功执行:

C:\ProgrammeNoSetup\ImageMagick-7.1.0-62-portable-Q16-HDRI-x64\magick.exe D:\orG.08_10-02_1.jpg -resize 1200x1200 D:\orG\output.08_10-02_1.jpg

但是,我尝试在PowerShell脚本(test.ps1)中使用以下代码执行:

$CMD = 'C:\ProgrammeNoSetup\ImageMagick-7.1.0-62-portable-Q16-HDRI-x64\magick.exe'
$arg2 = 'D:\orG.08_10-02_1.jpg'
$arg3 = '-resize 1200x1200'
$arg4 = 'D:\orG\output.08_10-02_1.jpg'

& $CMD $arg2 $arg3 $arg4

我收到以下错误信息:magick.exe: unrecognized option '-resize 1200x1200' at CLI arg 2 @ fatal/magick-cli.c/ProcessCommandOptions/659

我还尝试了以下方式:

$CMD = 'C:\ProgrammeNoSetup\ImageMagick-7.1.0-62-portable-Q16-HDRI-x64\magick.exe'
$arg1 = 'convert'
$arg2 = 'D:\orG.08_10-02_1.jpg'
$arg3 = '-resize 1200x1200'
$arg4 = 'D:\orG\output.08_10-02_1.jpg'

& $CMD $arg1 $arg2 $arg3 $arg4

但错误类似:convert: unrecognized option '-resize 1200x1200' @ error/convert.c/ConvertImageCommand/2686

如果我移除选项"-resize 1200x1200",则不会出现错误消息,命令将文件(未调整大小)写入输出文件夹。因此,我认为问题出在我的选项上,但我找不到它。我已经投入了几个小时的时间,还尝试了使用@符号作为参数,但没有成功。

英文:

I try to manipulate pictures with imageick. On the CLI I use this working fine command:

C:\ProgrammeNoSetup\ImageMagick-7.1.0-62-portable-Q16-HDRI-x64\magick.exe D:\orG.08_10-02_1.jpg -resize 1200x1200 D:\orG\output.08_10-02_1.jpg

But I try to that in a powershell script (test.ps1) with this code:

$CMD = 'C:\ProgrammeNoSetup\ImageMagick-7.1.0-62-portable-Q16-HDRI-x64\magick.exe'
$arg2 = 'D:\orG.08_10-02_1.jpg'
$arg3 = '-resize 1200x1200'
$arg4 = 'D:\orG\output.08_10-02_1.jpg'

& $CMD $arg2 $arg3 $arg4 

Im getting this error: magick.exe: unrecognized option `-resize 1200x1200' at CLI arg 2 @ fatal/magick-cli.c/ProcessCommandOptions/659.

I have also tried:

$CMD = 'C:\ProgrammeNoSetup\ImageMagick-7.1.0-62-portable-Q16-HDRI-x64\magick.exe'
$arg1 = 'convert'
$arg2 = 'D:\orG.08_10-02_1.jpg'
$arg3 = '-resize 1200x1200'
$arg4 = 'D:\orG\output.08_10-02_1.jpg'

& $CMD $arg1 $arg2 $arg3 $arg4 

But the error is similar: convert: unrecognized option `-resize 1200x1200' @ error/convert.c/ConvertImageCommand/2686.

If I remove the option "-resize 1200x1200". There is now error message and the command is writing the file (without resize) to the output folder. So I think, that I have a problem with my option, but I can't find it. I have invested several hours and als tried with the @-symbol for arguments, without success.

答案1

得分: 4

你必须分开参数名和值,否则-resize 1200x1200将被传递为单个标记,但本机可执行文件通常期望参数名和值为分开的标记(除非语法中没有空格,例如-name:value)。

$CMD = 'C:\ProgrammeNoSetup\ImageMagick-7.1.0-62-portable-Q16-HDRI-x64\magick.exe'
$arg1 = 'convert'
$arg2 = 'D:\orG.08_10-02_1.jpg'
$arg3 = '-resize', '1200x1200'
               # |<-- 这里插入了逗号
$arg4 = 'D:\orG\output.08_10-02_1.jpg'

& $CMD $arg1 $arg2 $arg3 $arg4 

这将把$arg3转换成包含两个字符串的数组,PowerShell将其作为分开的参数标记传递。这也被称为splatting,只有在调用本机可执行文件时才会执行此操作(对于PowerShell命令,您必须使用@ splatting运算符)。

另一种更简洁的方式:

$CMD = 'C:\ProgrammeNoSetup\ImageMagick-7.1.0-62-portable-Q16-HDRI-x64\magick.exe'

# 创建参数数组
$imArgs = @(
    'convert'
    'D:\orG.08_10-02_1.jpg'
    '-resize', '1200x1200'
    'D:\orG\output.08_10-02_1.jpg' 
)

# 调用本机命令,将参数数组展开
& $CMD $imArgs

# 或者您可以明确使用splatting:
& $CMD @imArgs

确保不要将变量命名为$args,因为这是保留的PowerShell变量。


另外,如果您希望能够调用ImageMagick而不必在所有脚本中硬编码其路径,请将ImageMagick的安装目录("C:\ProgrammeNoSetup\ImageMagick-7.1.0-62-portable-Q16-HDRI-x64")添加到PATH环境变量中。

然后,您可以通过指定其可执行文件的名称来调用ImageMagick:

magick @imArgs
英文:

You have to separate argument name and value, otherwise -resize 1200x1200 will be passed as a single token, but native executables usually expect argument name and value to be separate tokens (unless the syntax is without whitespace, e. g. -name:value).

$CMD = &#39;C:\ProgrammeNoSetup\ImageMagick-7.1.0-62-portable-Q16-HDRI-x64\magick.exe&#39;
$arg1 = &#39;convert&#39;
$arg2 = &#39;D:\orG.08_10-02_1.jpg&#39;
$arg3 = &#39;-resize&#39;, &#39;1200x1200&#39;
               # |&lt;-- inserted comma here
$arg4 = &#39;D:\orG\output.08_10-02_1.jpg&#39;

&amp; $CMD $arg1 $arg2 $arg3 $arg4 

This turns $arg3 into an array of two strings, which PowerShell passes as separate argument tokens. This is also called splatting and is only done when calling native executables (for PowerShell commands you have to use the @ splatting operator).

Another, more concise way:

$CMD = &#39;C:\ProgrammeNoSetup\ImageMagick-7.1.0-62-portable-Q16-HDRI-x64\magick.exe&#39;

# Create an array of arguments
$imArgs = @(
    &#39;convert&#39;
    &#39;D:\orG.08_10-02_1.jpg&#39;
    &#39;-resize&#39;, &#39;1200x1200&#39;
    &#39;D:\orG\output.08_10-02_1.jpg&#39; 
)

# Call native command, which splats argument array.
&amp; $CMD $imArgs

# Alternatively you can be explicit about splatting:
&amp; $CMD @imArgs

Make sure you don't name the variable $args which is a reserved PowerShell variable.


As an aside, if you want to be able to call ImageMagick without having to hardcode its path in all of your scripts, add the installation directory of ImageMagick ("C:\ProgrammeNoSetup\ImageMagick-7.1.0-62-portable-Q16-HDRI-x64") to the PATH environment variable.

You could then call ImageMagick simply by specifying the name of its executable:

magick @imArgs

huangapple
  • 本文由 发表于 2023年2月19日 18:15:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499385.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定