0

I wrote a script in PowerShell and I am having various success calling Image Magick's montage.exe on different computers. The computer on which I wrote the script has no problem executing the 'montage' command, however on another computer with IM Installed the script errors out:

montage : The term 'montage' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At \\Server\Contact_Sheet_Local.ps1:51 char:9
+         montage -verbose -label %t -pointsize 20 -background '#FFFFFF ...
+         ~~~~~~~
+ CategoryInfo          : ObjectNotFound: (montage:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

I have tried using montage.exe and even tried the entire path C:\Program Files\ImageMagick-7.0.3-Q16\montage.exe. Additionally I tried setting the directory first:

Set-Location -Path 'C:\Program Files\ImageMagick-7.0.3-Q16'
montage...

Each time on a particular computer this fails. I have tried with IM versions 7.0.3-Q16 and 6.9.1-6-Q8 both x64 as both computers are x64

In the past, I have created scripts in .bat that use ImageMagick and I had to define the full path to the .exe as I mentioned above. But this doesn't seem to help in PowerShell.

Does anyone have any advice or experience with this problem?

4

2 回答 2

0

如果您的路径有空格,那么如果您只是尝试基于它执行,它将失败。您需要使用点运算符

$Exe = 'C:\Program Files\ImageMagick-6.9.1-6-Q8\montage.exe'
If (-not (Test-Path -Path $Exe))
{
    $Exe = 'C:\Program Files\ImageMagick-7.0.3-Q16\montage.exe'
}
. $Exe -arg1 -etc
于 2017-11-14T19:01:40.463 回答
0

默认情况下,PowerShell 不执行当前目录中的程序。如果要运行位于当前目录中的可执行文件,请在可执行文件的名称前加上.\or ./。例子:

Set-Location "C:\Program Files\ImageMagick-7.0.3-Q16"
.\montage.exe ...

如果您在字符串或字符串变量中有可执行文件的名称并且想要执行它,您可以使用&(call or invocation) 运算符执行此操作:

& "C:\Program Files\ImageMagick-7.0.3-Q16\montage.exe" ...

如果您指定的路径和文件名不包含空格,&则不需要运算符;例子:

C:\ImageMagick\montage.exe ...

你也可以这样写:

& C:\ImageMagick\montage.exe ...

如果您在字符串变量中有可执行文件的文件名并且想要执行它,请使用&; 例子:

$execName = "C:\Program Files\ImageMagick-7.0.3-Q16\montage.exe"
& $execName ...
于 2017-11-14T19:35:16.017 回答