← 返回 Skills 市场
jeremycooper2077

Pwsh Encoding Fix

作者 JeremyCooper2077 · GitHub ↗ · v1.0.0 · MIT-0
cross-platform ⚠ suspicious
39
总下载
0
收藏
0
当前安装
1
版本数
在 OpenClaw 中安装
/install pwsh-encoding-fix
功能描述
Fix PowerShell file encoding corruption (backtick, dollar sign, Chinese char mangling) when writing Node.js files on Windows.
使用说明 (SKILL.md)

PowerShell 文件编码修复

在 Windows PowerShell 中写入含特殊字符的 Node.js/JS 文件时,PowerShell 会破坏文件内容。此 Skill 提供诊断和修复方法。

现象

问题 表现 原因
反引号被吞 模板字符串 `hello ${name}`"hello ${name}" PowerShell 把 ` 当转义符
$ 变量被展开 ${var} 变空字符串 PowerShell 展开 $var 变量
中文变乱码 汉字变垃圾字符(如 已查到 UTF-8 被 GBK 重编码
文件体积暴增 13KB → 20MB 乱码字符被反复替换

根本原因

PowerShell 管道命令会修改文件编码:

# ❌ 这些命令会破坏文件:
Set-Content file.js "content"         # 吞反引号
Out-File file.js                       # 吞反引号
Get-Content | Set-Content             # 管道 → 重编码
Write-Output "`$var" > file.js        # 展开 $ 变量

使用诊断修复工具

本 Skill 附带一个可执行脚本 pwsh-encoding-fix.js,可以在任何有 Node.js 的 Windows 机器上运行:

# 诊断文件编码问题
node pwsh-encoding-fix.js \x3C文件路径>

# 诊断并自动修复(仅 GBK 乱码可自动修复)
node pwsh-encoding-fix.js \x3C文件路径> --fix

工具的诊断能力

严重度 检测类型 说明
!! CRITICAL BACKTICK_LOST 模板字符串反引号被 PowerShell 吞掉
!! CRITICAL DOLLAR_EXPANDED ${var} 变量被展开为空
!! CRITICAL GBK_CORRUPT 中文 UTF-8 被 GBK 重编码损坏(可自动修复)
!! CRITICAL SYNTAX_ERROR JS 语法检查失败,可能是编码损坏导致
WW WARNING BACKTICK_PARTIAL 部分模板可能受损
WW WARNING GBK_CORRUPT (mild) 部分中文可能受损
II LOW BOM 文件含 UTF-8 BOM 头(不影响运行但不够干净)

工具输出示例

========================================================
  PowerShell Encoding Damage Report
========================================================
  File:    C:\project\server.js
  Size:    17.9 KB  (408 lines)
  Backtick: 0
  Dollar{}: 15 total
  Suspicious lines: 12 / 12
  Chinese: 1257 valid / 242 GBK residual
  Syntax:  FAIL
--------------------------------------------------------
  Found 3 issue(s):

  !! [CRITICAL] BACKTICK_LOST
     12 lines use ${} without backticks. Template literals stripped.
  !! [CRITICAL] GBK_CORRUPT
     GBK residual: 242 chars / valid Chinese: 1257 (ratio 0.2)
  !! [CRITICAL] SYNTAX_ERROR
     Unexpected token ...
--------------------------------------------------------
  Critical issues found. Use --fix to attempt repair.

安全写入方法

方法 1:Node.js writeFileSync(最安全)

node -e "fs.writeFileSync('file.js', 'content', 'utf8')"

注意:-e 字符串中的 $ 仍会被 PowerShell 展开,需要用单引号包裹或转义。

方法 2:OpenClaw write 工具(推荐)

直接使用 write 工具创建文件,不走 PowerShell 管道。

方法 3:hex 编码写入(万无一失)

在干净机器上生成 hex:

node -e "const fs=require('fs');const h=fs.readFileSync('file.js').toString('hex');console.log(h)" > file.hex

在目标机器上恢复:

node -e "const fs=require('fs');fs.writeFileSync('file.js',Buffer.from(fs.readFileSync('file.hex','utf8').trim(),'hex'))"

诊断命令(手动)

# 检查语法
node --check file.js

# 检查 BOM 头(正确的文件头应是 63 6f 6e = "con")
node -e "const fs=require('fs');const b=fs.readFileSync('file.js');console.log(b[0].toString(16),b[1].toString(16),b[2].toString(16))"

# 检查反引号数量
node -e "const fs=require('fs');const c=fs.readFileSync('file.js','utf8');const bt=String.fromCharCode(96);console.log('Backtick count:',c.split(bt).length-1)"

修复步骤

  1. 不要用 PowerShell 管道修复! 不要 Get-Content | Set-Content
  2. 用 Node.js 读取原文件:fs.readFileSync('file.js', 'utf8')
  3. 检查编码损坏类型(反引号缺失 / 中文乱码 / 体积暴增)
  4. 如果只是中文乱码:用 --fix 模式自动修复,或用 find/replace 映射
  5. 如果反引号也被吃了:必须从原始源码重写
  6. 最稳妥:用 write 工具直接从原始源码完整重写

预防措施

  • 用 OpenClaw write 工具代替 PowerShell 写文件
  • 改完文件后立刻 node --check 验证语法
  • 复杂 JS 文件用 Node.js writeFileSync 或 hex 编码写入

文件

路径 说明
SKILL.md 本文档
pwsh-encoding-fix.js 可执行诊断修复工具(Node.js脚本)

在其他机器上部署

只需将 pwsh-encoding-fix.js 拷贝到目标机器(任何 Windows 系统),确保已安装 Node.js:

# 从本目录复制到目标机器
# 然后在目标机器上运行
node pwsh-encoding-fix.js 需要检查的文件.js
node pwsh-encoding-fix.js 需要检查的文件.js --fix

无需安装任何 npm 包。GBK 修复需要 iconv-lite(自动检测,无则回退到内置 TextDecoder)。

安全使用建议
Install only if you need a local PowerShell encoding diagnostic/repair helper. Run it on files you control, avoid unusual or attacker-supplied file paths, and use `--fix` only after you are comfortable with it rewriting the target file while creating a `.bak` backup.
能力评估
Purpose & Capability
The stated purpose matches the artifacts: it diagnoses PowerShell-related JavaScript file corruption, reads a user-specified file, runs a JavaScript syntax check, and optionally repairs GBK corruption.
Instruction Scope
The diagnostic path is user-directed, but the script constructs a shell command from the provided file path for `node --check`, which is avoidable and under-scoped compared with safer argument-based execution.
Install Mechanism
The skill is a standalone markdown guide plus one Node.js script, with no declared install hooks, package installation, or network setup.
Credentials
Local file reads and optional writes are proportionate for an encoding repair utility, but users should only run it on intended files and trusted paths.
Persistence & Privilege
There is no background persistence or privilege escalation; `--fix` creates a `.bak` file and rewrites the target file, which is disclosed but mutates user data.
如何使用
  1. 确保已安装 OpenClaw(本地或 Docker 部署)
  2. 在对话框中输入安装命令:/install pwsh-encoding-fix
  3. 安装完成后,直接呼叫该 Skill 的名称或使用 /pwsh-encoding-fix 触发
  4. 根据 Skill 的参数说明提供必要输入,即可获得结构化输出
版本历史
v1.0.0
Initial release: PowerShell编码损坏检测与修复工具, 支持反引号丢失/变量展开空/GBK乱码/语法错误/BOM头诊断
元数据
Slug pwsh-encoding-fix
版本 1.0.0
许可证 MIT-0
累计安装 0
当前安装数 0
历史版本数 1
常见问题

Pwsh Encoding Fix 是什么?

Fix PowerShell file encoding corruption (backtick, dollar sign, Chinese char mangling) when writing Node.js files on Windows. 它是一个面向 Claude Code / OpenClaw 的 AI Agent Skill 插件,目前累计下载 39 次。

如何安装 Pwsh Encoding Fix?

在 OpenClaw 或 Claude Code 对话框中运行命令「/install pwsh-encoding-fix」即可一键安装,无需额外配置。

Pwsh Encoding Fix 是免费的吗?

是的,Pwsh Encoding Fix 完全免费,采用 MIT-0 许可证,可自由下载、安装和使用。

Pwsh Encoding Fix 支持哪些平台?

Pwsh Encoding Fix 跨平台运行,可在任意部署了 OpenClaw / Claude Code 的环境中使用(cross-platform)。

谁开发了 Pwsh Encoding Fix?

由 JeremyCooper2077(@jeremycooper2077)开发并维护,当前版本 v1.0.0。

💬 留言讨论