node.js - 在 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%)
- 为什么异常只显示在 ISE 中而不显示在 powershell 中?另一个类似的答案表明控制台没有打开严格模式,所以我运行
Set-StrictMode -Version latest
并重新运行命令,但仍然没有看到异常 - 是什么原因以及如何解决?
解决方案
推荐阅读
- vba - ADODB 导入 .csv 更改数据类型
- amazon-aurora - 如何查看 aws aurora 模式中已使用和可用的总空间
- git - Git 错误,无法访问,错误设置证书验证位置
- spring - 自定义 Spring Boot Starter 库中读取当前属性并根据值设置新属性的最佳方式
- java - Java DateTime 在 Linux 中为空
- lua - 使用非整数键删除表
- jakarta-ee - 在 WSO2 迭代调解器上定义的这个 XPATH 表达式究竟是如何工作的?
- powershell - 检查远程机器的已安装程序不起作用
- android - 在调用阶段使用 Native C Sinch Client 的内存泄漏
- vb.net - 带有重定向到文件的 Shell 命令