首页 > 解决方案 > 在 Powershell ISE 中运行时,工作的 Powershell 命令报告异常

问题描述

我正在尝试编写一个使用 Squoosh CLI 批量调整图像大小的 powershell 脚本。

由于Squoosh CLI需要 npm 安装,所以我在 windows 上安装了 npm 并安装了 Squoosh CLI

squoosh.app可以从其 Web UI 生成 Squoosh CLI 命令,所以我从那里得到了我的 squoosh 命令:

npx @squoosh/cli --resize '{"enabled":true,"width":2017,"height":1350,"method":"lanczos3","fitMethod":"stretch","premultiply":true,"linearRGB":true}' 
--mozjpeg '{"quality":75,"baseline":false,"arithmetic":false,"progressive":true,"optimize_coding":true,"smoothing":0,"color_space":3,"quant_table":3,"trellis_multipass":false,"trellis_opt_zero":false,"trellis_opt_table":false,"trellis_loops":1,"auto_subsample":true,"chroma_subsample":2,"separate_chroma_quality":false,"chroma_quality":75}'

我进行了一些更改,以便可以将其粘贴到 powershell 以调整图像大小:

$DirectoryName = "F:\TEMP\images"
$Fullname = "F:\TEMP\images\01.jpeg"
squoosh-cli -s _squoosh -d $DirectoryName --resize '{"enabled":true,"height":1200,"method":\"lanczos3\","fitMethod":\"stretch\","premultiply":true,\"linearRGB\":true}' --mozjpeg '{"quality":75,"baseline":false,"arithmetic":false,"progressive":true,"optimize_coding":true,"smoothing":0,"color_space":3,"quant_table":3,"trellis_multipass":false,"trellis_opt_zero":false,"trellis_opt_table":false,"trellis_loops":1,"auto_subsample":true,"chroma_subsample":2,"separate_chroma_quality":false,"chroma_quality":75}' $Fullname

请注意 json 参数值被转义,因为我注意到 powershell 在 处报告错误"lanczos3",\"fitMethod\",因为fitMethod它是一个字符串值并且它被双引号包围,powershell 抱怨。所以我只是逃脱了整个json。它在powershell中工作。

但是当我将相同的脚本复制到powershell ISE时,尽管图像已成功压缩,但它会报告错误。

PS C:\Users\Administrator> $DirectoryName = "F:\TEMP\images"
$Fullname = "F:\TEMP\images\01.jpeg"
squoosh-cli -s _squoosh -d $DirectoryName --resize '{"enabled":true,"height":1200,"method":\"lanczos3\","fitMethod":\"stretch\","premultiply":true,\"linearRGB\":true}' --mozjpeg '{"quality":75,"baseline":false,"arithmetic":false,"progressive":true,"optimize_coding":true,"smoothing":0,"color_space":3,"quant_table":3,"trellis_multipass":false,"trellis_opt_zero":false,"trellis_opt_table":false,"trellis_loops":1,"auto_subsample":true,"chroma_subsample":2,"separate_chroma_quality":false,"chroma_quality":75}' $Fullname
node.exe : 1/1 √ Squoosh results:
所在位置('position' in EN) C:\Users\Administrator\AppData\Roaming\npm\squoosh-cli.ps1:15 字符('character' in EN): 3
+   & "node$exe"  "$basedir/node_modules/@squoosh/cli/build/index.js" $ ...
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (1/1 √ Squoosh results::String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
 F:\TEMP\images\01.jpeg: 842.72KB
  └ _squoosh.jpg → 92.33KB (11.0%)
  1. 为什么异常只显示在 ISE 中而不显示在 powershell 中?另一个类似的答案表明控制台没有打开严格模式,所以我运行Set-StrictMode -Version latest并重新运行命令,但仍然没有看到异常
  2. 是什么原因以及如何解决?

标签: node.jsjsonpowershellnpmescaping

解决方案


推荐阅读