在 GUI 支持下过度使用 Ingo Karstein 的出色脚本。 GUI 输出和输入通过一个开关激活,生成真正的 Windows 可执行文件。仅与 Powershell 5.x 一起编译。带有可选的图形前端Win-PS2EXE。
模块版本。
您可以在此处找到基于脚本的版本 (https://github.com/MScholtes/TechNet-Gallery)。
作者:马库斯·斯科尔斯
版本:1.0.14
日期:2024年9月15日
PS C: > Install-Module ps2exe
或从此处下载:https://www.powershellgallery.com/packages/ps2exe/。
Invoke-ps2exe .source.ps1 . target.exe
或者
ps2exe .source.ps1 . target.exe
将“source.ps1”编译为可执行文件target.exe(如果省略“.target.exe”,则输出将写入“.source.exe”)。
或启动 Win-PS2EXE 以获得图形前端
Win - PS2EXE
ps2exe [ - inputFile ] ' ' [[ - outputFile ] ' ' ]
[ - prepareDebug ] [ - x86 | - x64 ] [ - lcid < id > ] [ - STA | - MTA ] [ - noConsole ] [ - UNICODEEncoding ]
[ - credentialGUI ] [ - iconFile ' ' ] [ - title ' ' ] [ - description ' ' ]
[ - company ' ' ] [ - product ' ' ] [ - copyright ' ' ] [ - trademark ' ' ]
[ - version ' ' ] [ - configFile ] [ - noOutput ] [ - noError ] [ - noVisualStyles ] [ - exitOnCancel ]
[ - DPIAware ] [ - requireAdmin ] [ - supportOS ] [ - virtualize ] [ - longPaths ]
inputFile = Powershell script that you want to convert to executable (file has to be UTF8 or UTF16 encoded)
outputFile = destination executable file name or folder, defaults to inputFile with extension '.exe'
prepareDebug = create helpful information for debugging
x86 or x64 = compile for 32-bit or 64-bit runtime only
lcid = location ID for the compiled executable. Current user culture if not specified
STA or MTA = 'Single Thread Apartment' or 'Multi Thread Apartment' mode
noConsole = the resulting executable will be a Windows Forms app without a console window
UNICODEEncoding = encode output as UNICODE in console mode
credentialGUI = use GUI for prompting credentials in console mode
iconFile = icon file name for the compiled executable
title = title information (displayed in details tab of Windows Explorer's properties dialog)
description = description information (not displayed, but embedded in executable)
company = company information (not displayed, but embedded in executable)
product = product information (displayed in details tab of Windows Explorer's properties dialog)
copyright = copyright information (displayed in details tab of Windows Explorer's properties dialog)
trademark = trademark information (displayed in details tab of Windows Explorer's properties dialog)
version = version information (displayed in details tab of Windows Explorer's properties dialog)
configFile = write config file (.exe.config)
noOutput = the resulting executable will generate no standard output (includes verbose and information channel)
noError = the resulting executable will generate no error output (includes warning and debug channel)
noVisualStyles = disable visual styles for a generated windows GUI application (only with -noConsole)
exitOnCancel = exits program when Cancel or "X" is selected in a Read-Host input box (only with -noConsole)
DPIAware = if display scaling is activated, GUI controls will be scaled if possible (only with -noConsole)
requireAdmin = if UAC is enabled, compiled executable run only in elevated context (UAC dialog appears if required)
supportOS = use functions of newest Windows versions (execute [Environment]::OSVersion to see the difference)
virtualize = application virtualization is activated (forcing x86 runtime)
longPaths = enable long paths ( > 260 characters) if enabled on OS (works only with Windows 10)
生成的可执行文件具有以下保留参数:
-? [] Powershell help text of the script inside the executable. The optional parameter combination
"-? -detailed", "-? -examples" or "-? -full" can be used to get the appropriate help text.
-debug Forces the executable to be debugged. It calls "System.Diagnostics.Debugger.Launch()".
-extract: Extracts the powerShell script inside the executable and saves it as FILENAME.
The script will not be executed.
-wait At the end of the script execution it writes "Hit any key to exit..." and waits for a key to be pressed.
-end All following options will be passed to the script inside the executable.
All preceding options are used by the executable itself and will not be passed to the script.
PS2EXE 的基本输入/输出命令必须用 C# 重写。未实现的是控制台模式下的Write-Progress (工作量太大)和Start-Transcript / Stop-Transcript (Microsoft 没有适当的参考实现)。
默认情况下,在 powershell 中,命令行开关的输出按行格式化(作为字符串数组)。当您的命令生成 10 行输出并且您使用 GUI 输出时,将出现 10 个消息框,每个消息框等待确定。为了防止这种情况,请将您的命令传送到comandlet Out-String。这会将输出转换为一个包含 10 行的字符串数组,所有输出将显示在一个消息框中(例如:dir C: | Out-String)。
PS2EXE 可以使用生成的可执行文件的名称+“.config”创建配置文件。在大多数情况下,这些配置文件不是必需的,它们是一个清单,告诉应使用哪个 .Net Framework 版本。由于您通常会使用实际的 .Net Framework,因此请尝试在没有配置文件的情况下运行可执行文件。
编译的脚本像原始脚本一样处理参数。 Windows 环境存在一项限制:对于所有可执行文件,所有参数的类型均为 STRING,如果参数类型没有隐式转换,则必须在脚本中显式转换。您甚至可以使用相同的限制将内容通过管道传送到可执行文件(所有管道传送的值都具有 STRING 类型)。
切勿在编译的脚本中存储密码!可以简单地使用参数 -extract 反编译脚本。例如
Output.exe - extract:C:Output.ps1
将反编译存储在 Output.exe 中的脚本。
由于 PS2EXE 将脚本转换为可执行文件,因此与脚本相关的变量不再可用。特别是变量 $PSScriptRoot 为空。
变量 $MyInspiration 设置为脚本中以外的其他值。
您可以使用以下代码检索独立于已编译/未编译的脚本/可执行路径(感谢 JacquesFS):
if ( $MyInvocation .MyCommand.CommandType -eq " ExternalScript " )
{ $ScriptPath = Split-Path - Parent - Path $MyInvocation .MyCommand.Definition }
else
{ $ScriptPath = Split-Path - Parent - Path ([ Environment ]::GetCommandLineArgs()[ 0 ])
if ( ! $ScriptPath ){ $ScriptPath = " . " } }
当使用 -noConsole 模式在脚本中打开外部窗口时(即,对于 Get-Credential 或需要 cmd.exe shell 的命令),下一个窗口将在后台打开。
原因是关闭外部窗口时窗口会尝试激活父窗口。由于编译后的脚本没有窗口,因此会激活编译后脚本的父窗口,通常是 Explorer 或 Powershell 的窗口。
为了解决这个问题,$Host.UI.RawUI.FlushInputBuffer() 打开一个可以激活的不可见窗口。以下调用 $Host.UI.RawUI.FlushInputBuffer() 将关闭此窗口(依此类推)。
以下示例将不再在后台打开窗口,因为只需调用“ipconfig | Out-String”即可:
$Host .UI.RawUI.FlushInputBuffer ()
ipconfig | Out-String
$Host .UI.RawUI.FlushInputBuffer ()
将文件从 UTF-16 转换为 UTF-8 以允许 git diff
在控制台模式下忽略安全字符串请求中的控制键