在 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
在控制台模式下忽略安全字串請求中的控制鍵