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的命令行参数也会被完整地传递给这个临时脚本。 - 清理:脚本执行完毕后,引导程序会自动删除所有临时文件,清理执行环境。
许可证
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
psh2bat-0.0.7.tar.gz
(24.2 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
psh2bat-0.0.7-py3-none-any.whl
(24.4 kB
view details)
File details
Details for the file psh2bat-0.0.7.tar.gz.
File metadata
- Download URL: psh2bat-0.0.7.tar.gz
- Upload date:
- Size: 24.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
10805ba5e26de0d1b73cd4d4c40553b09eae8da18892f79bd78745d58128f5e6
|
|
| MD5 |
23ed60e9233348049b1436b33f20e8a7
|
|
| BLAKE2b-256 |
1f91a07b45cc06b51d8f4e57162a4b29fd447109bfe0430c2a92ddf656e6e185
|
File details
Details for the file psh2bat-0.0.7-py3-none-any.whl.
File metadata
- Download URL: psh2bat-0.0.7-py3-none-any.whl
- Upload date:
- Size: 24.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb858071d2259f0b43c7ea86b0e6437f412d1769c8c97805b1da1b222b863b66
|
|
| MD5 |
4a91857f951da4582c8f72eb3b3a7457
|
|
| BLAKE2b-256 |
599e3b7bee684c79f5786a229d5d5a177a5c22fe654a6ccc001348aaa608f65d
|