Psh2Bat
简介
将 PowerShell 脚本转换为 Bat 脚本的工具。
因为不想写 Bat 脚本所以写了这个工具。
安装
确保您的系统已安装 Python 3.10+。
pip install psh2bat
使用
PowerShell 转 Bat
psh2bat <PowerShell 脚本路径> --output-path <导出路径>
Bat 转 PowerShell
psh2bat <Bat 脚本路径> --output-path <导出路径> --reverse
[!IMPORTANT]
仅支持由 Psh2Bat 转换而来的 Bat 脚本。
实现原理
Psh2Bat 并非将 PowerShell 脚本的语法逐行翻译成等效的 Batch (CMD) 语法,而是采用了一种更为巧妙的“嵌入式”打包策略。它生成一个独立的 .bat 文件,该文件内部包含了原始的 PowerShell 脚本,并通过一系列步骤来调用 powershell.exe 执行它。
1. 生成混合脚本
当转换一个 .ps1 脚本时,工具会:
- 读取原始脚本:将 PowerShell 脚本的全部内容读取为字符串。
- 构建模板:Psh2Bat 内部有一个预定义的
.bat脚本模板。此模板是一个“三段式”结构,同时包含了 Batch 启动代码、一个 PowerShell 引导程序和用于注入用户脚本的占位符。不同的部分由独特的随机标记(例如:__PowerShellCodeExec_...__:)分隔,以避免冲突。 - 注入代码:将用户原始的 PowerShell 代码注入到模板的指定位置。
最终生成的 .bat 文件结构如下:
@echo off
:: 第一部分:Batch 启动代码
:: ...
:: 核心是调用 powershell.exe,读取并执行文件自身的第二部分
cmd /c "powershell -ExecutionPolicy Bypass ... [scriptblock]::Create($f[1]) ..."
goto :ExitCode
:__PowerShellCodeExec_...__:
# 第二部分:PowerShell 引导代码
# ...
# 它的任务是从本文件中提取出第三部分(用户脚本)
# ...
# 然后将其写入一个临时的 .ps1 文件
Set-Content -Value $psh_code -Path $temp_script_path
# 最后执行这个临时脚本
Invoke-Expression "& `"$temp_script_path`" $(Get-ExtraArgs)"
# ...
:__PowerShellCode_...__:
# 第三部分:用户原始的 PowerShell 脚本被注入于此
# ...
:__PowerShellCode_...__:
:ExitCode
exit /b %_psh_exit_code_%
2. 执行流程
当用户运行这个生成的 .bat 文件时:
- Batch 启动:文件顶部的 Batch 代码首先被执行。
- 调用引导程序:Batch 代码通过
powershell.exe执行嵌入在文件第二部分的 PowerShell “引导程序”。 - 提取并执行主脚本:引导程序从文件自身提取出第三部分的“主脚本”(即用户原始的 PowerShell 代码),将其写入一个临时
.ps1文件,然后执行这个临时文件。所有传递给.bat的命令行参数也会被完整地传递给这个临时脚本。 - 清理:脚本执行完毕后,引导程序会自动删除所有临时文件,清理执行环境。
许可证
Metadata
Release files for psh2bat 0.0.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| psh2bat-0.0.7.tar.gz | 24.2 kB | Without provenance | Details for psh2bat-0.0.7.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| psh2bat-0.0.7-py3-none-any.whl | Python 3 | none | any | Details for psh2bat-0.0.7-py3-none-any.whl |
Total release size: 48.6 kB
Release files / psh2bat-0.0.7.tar.gz
| Download URL | psh2bat-0.0.7.tar.gz |
|---|---|
| Size | 24.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
10805ba5e26de0d1b73cd4d4c40553b09eae8da18892f79bd78745d58128f5e6
|
|
BLAKE2b-256 checksum How to use checksums |
1f91a07b45cc06b51d8f4e57162a4b29fd447109bfe0430c2a92ddf656e6e185
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.13
|
Release files / psh2bat-0.0.7-py3-none-any.whl
| Download URL | psh2bat-0.0.7-py3-none-any.whl |
|---|---|
| Size | 24.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cb858071d2259f0b43c7ea86b0e6437f412d1769c8c97805b1da1b222b863b66
|
|
BLAKE2b-256 checksum How to use checksums |
599e3b7bee684c79f5786a229d5d5a177a5c22fe654a6ccc001348aaa608f65d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.13
|
Security for psh2bat 0.0.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.