Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

powershell - Using the call '&' operator with multiple parameters

            $sImageMagickHome = "C:ImageMagick"
            $sImageMagickConv = "$sImageMagickHomeconvert.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:ImageMagickconvert.exe --% -background transparent -fill hsb(0,0,0) -font Arial -pointsize 18 -size 18x26 -gravity center label:? C:Userserics_000DesktopOutputChars?.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...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The following script works for me:

$sImageMagickHome = "C:devim"
$sImageMagickConv = "$sImageMagickHomeconvert.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...