Using the call '&' operator with multiple parameters

╄→гoц情女王★ 提交于 2021-02-13 17:35:52

问题


            $sImageMagickHome = "C:\ImageMagick"
            $sImageMagickConv = "$sImageMagickHome\convert.exe"
            $sImageMagickArgs = @(  '--%', 
                            '-background transparent', 
                            '-fill hsb(0,0,0)', 
                            '-font Arial',
                            '-pointsize 18',
                            '-size 18x26',
                            '-gravity center')


            for ( $i = 0x01; $i -le 0x05; $i++ )
            {
                $y = [char]$i
                & $sImageMagickConv $sImageMagickArgs label:$y $sCharsDir\$y.png
                #Write-Host $sImageMagickConv $sImageMagickArgs label:$y $sCharsDir\$y.png
            }

Using Write-Host I can get an example to copy paste into the command line and I find it does run correctly if I run this single line from the PowerShell prompt:

C:\ImageMagick\convert.exe --% -background transparent -fill hsb(0,0,0) -font Arial -pointsize 18 -size 18x26 -gravity center label:☺ C:\Users\erics_000\Desktop\Output\Chars\☺.png

Using the call operator '&' from inside the script does not work at all however and leads to some error messages:

convert.exe: UnableToOpenBlob `--%': No such file or directory @ error/blob.c/OpenBlob/2697.
convert.exe: NoDecodeDelegateForThisImageFormat `' @ error/constitute.c/ReadImage/501.
convert.exe: UnrecognizedOption `-background transparent' @ error/convert.c/ConvertImageCommand/858.

The article I have been reading is: http://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx

Thank you...


回答1:


The following script works for me:

$sImageMagickHome = "C:\dev\im"
$sImageMagickConv = "$sImageMagickHome\convert.exe"
$sImageMagickArgs = @('-background', 'transparent', 
                '-fill', 'hsb(0,0,0)', 
                '-font', 'Arial',
                '-pointsize', '18',
                '-size', '18x26',
                '-gravity', 'center')


for ( $i = 65; $i -le 67; $i++ )
{
    $y = [char]$i
    & $sImageMagickConv $sImageMagickArgs label:$y c:\dev\$y.bmp
}

Note that you cannot just Write-Host the arguments and try running it from command line, Powershell does special processing for & operator (adds quotes where needed), which it does not when you pass same arguments to Write-Host.

You'd probably want to install PSCX and play around with echoargs utility bundled with it to gain better understanding of how arguments are passed.



来源:https://stackoverflow.com/questions/31348736/using-the-call-operator-with-multiple-parameters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!