Skip to main content

将 PowerShell 脚本嵌入 Bat 脚本的工具。

Project description

Psh2Bat


简介

将 PowerShell 脚本转换为 Bat 脚本的工具。

因为不想写 Bat 脚本所以写了这个工具。

安装

确保您的系统已安装 Python 3.10+。

pip install psh2bat

使用

PowerShell 转 Bat

psh2bat <Bat 脚本路径> --output-path <导出路径>

Bat 转 PowerShell

psh2bat <Bat 脚本路径> --output-path <导出路径> --reverse

[!IMPORTANT]
仅支持由 Psh2Bat 转换而来的 Bat 脚本。

实现原理

Psh2Bat 并非将 PowerShell 脚本的语法逐行翻译成等效的 Batch (CMD) 语法,而是采用了一种更为巧妙的“嵌入式”打包策略。它生成一个独立的 .bat 文件,该文件内部包含了原始的 PowerShell 脚本,并通过一系列步骤来调用 powershell.exe 执行它。

1. 生成混合脚本

当转换一个 .ps1 脚本时,工具会:

  1. 读取原始脚本:将 PowerShell 脚本的全部内容读取为字符串。
  2. 构建模板:Psh2Bat 内部有一个预定义的 .bat 脚本模板。此模板是一个“三段式”结构,同时包含了 Batch 启动代码、一个 PowerShell 引导程序和用于注入用户脚本的占位符。不同的部分由独特的随机标记(例如 :__PowerShellCodeExec_...__:)分隔,以避免冲突。
  3. 注入代码:将用户原始的 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 文件时:

  1. Batch 启动:文件顶部的 Batch 代码首先被执行。
  2. 调用引导程序:Batch 代码通过 powershell.exe 执行嵌入在文件第二部分的 PowerShell “引导程序”。
  3. 提取并执行主脚本:引导程序从文件自身提取出第三部分的“主脚本”(即用户原始的 PowerShell 代码),将其写入一个临时 .ps1 文件,然后执行这个临时文件。所有传递给 .bat 的命令行参数也会被完整地传递给这个临时脚本。
  4. 清理:脚本执行完毕后,引导程序会自动删除所有临时文件,清理执行环境。

许可证

Project details


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.1.tar.gz (23.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

psh2bat-0.0.1-py3-none-any.whl (23.3 kB view details)

Uploaded Python 3

File details

Details for the file psh2bat-0.0.1.tar.gz.

File metadata

  • Download URL: psh2bat-0.0.1.tar.gz
  • Upload date:
  • Size: 23.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for psh2bat-0.0.1.tar.gz
Algorithm Hash digest
SHA256 e356099a8d632b9b298f64127783ae31cf737a7e5c173a432d292ef267b93a81
MD5 fd261de8d144d069094baeed0d6e9e92
BLAKE2b-256 131ffba4f7330318a3dc1252ee78983300860de80cda22b9862a20ee01d42197

See more details on using hashes here.

File details

Details for the file psh2bat-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: psh2bat-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 23.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for psh2bat-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 67acfedddf2e9af220a2d925a25a2014a74da9c66799435c8ac44a7ab6d504d3
MD5 98df5f570024f4dbe3f0f475c0dedb07
BLAKE2b-256 95506c02c96da01eae3653aff22965642f60d80ed0192d9fe5cd4326f9c42755

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page