使用呼叫“&"具有多个参数的运算符 [英] Using the call '&' operator with multiple parameters

查看: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
            }

使用Write-Host,我可以得到一个将粘贴复制到命令行的示例,如果从PowerShell提示符运行以下一行,我发现它确实可以正常运行:

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.

我一直在阅读的文章是: http://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx

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

谢谢...

推荐答案

以下脚本对我有用:

$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
}

请注意,您不能只是 Write-Host 参数并尝试从命令行运行它,Powershell会对& 运算符进行特殊处理(在需要的地方添加引号),当您将相同的参数传递给 Write-Host 时,它不会.

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.

您可能想要安装 PSCX 并使用 echoargs 实用程序与它捆绑在一起,可以更好地了解如何传递参数.

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.

这篇关于使用呼叫“&"具有多个参数的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆