PowerShellのps1ファイルをexeに変換するコマンド「ps2exe」

御殿山 コンピュータ
出典:国立国会図書館「NDLイメージバンク」
PowerShellのps1ファイルをexeにコンパイル?してくれるコマンドをPowerShell Galleryで見つけましたので試してみます。
WindowsPowerShell(PowerShell5)で実行しています。powershell7ではインストールは出来ましたが引数なしのコンパイルは失敗しました。

インストール

PS>Install-Module ps2exe

引数なしで実行

PS2EXE-GUI v0.5.0.28 by Ingo Karstein, reworked and GUI support by Markus Scholtes

Usage:

Invoke-ps2exe [-inputFile] '<filename>' [[-outputFile] '<filename>']
              [-prepareDebug] [-x86|-x64] [-lcid <id>] [-STA|-MTA] [-noConsole] [-UNICODEEncoding]
              [-credentialGUI] [-iconFile '<filename>'] [-title '<title>'] [-description '<description>']
              [-company '<company>'] [-product '<product>'] [-copyright '<copyright>'] [-trademark '<trademark>']
              [-version '<version>'] [-configFile] [-noOutput] [-noError] [-noVisualStyles] [-exitOnCancel]
              [-DPIAware] [-winFormsDPIAware] [-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 a config file (<outputfile>.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
winFormsDPIAware = if display scaling is activated, WinForms use DPI scaling (requires Windows 10 and .Net 4.7 or up)
    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 or up)

Input file not specified!

ps1スクリプトをexeファイル化

ファイル名:hwllo.ps1

<#
.SYNOPSIS
 hello powershell

.PARAMETER Help
 "hello powershell"とコンソールにメッセージを表示

#>

Param(
    [switch]$Help
)

if ($Help)
{
    Get-Help $PSCommandPath
    Exit 1
}

Write-Host "hello powershell"

変換

PS>ps2exe ./hello.ps1 ./hello.exe

hello.exeが出来上がった。

hello.exeファイルを実行

PS>./hello.exe
hello powershell

一瞬結果が出るまで待たせられる感じがしますが、成功しています。

hello.exeファイルに-helpをつけて実行

PS>./hello.exe -help


トピック
    Windows PowerShell のヘルプ システム

概要
    Windows PowerShell のコマンドレットと概念に関するヘルプを表示します。

詳細説明
    Windows PowerShell ヘルプでは、Windows PowerShell コマンドレット、
    関数、スクリプト、およびモジュールと、Windows PowerShell 言語の
    要素などの概念について説明します。 ...以下省略

失敗しています。

hello.exeの$PSCommandPath

$PSCommandPathの文字列の長さは0でした。helpの出力の内容は引数なしのget-helpが出力されている模様

感想

簡単なスクリプトを試しただけですが確かにexeファイルが出来上がっています。

個人的にexe化で何が嬉しいかと言いますと、.ps1を実行するショートカットを作る場合powershell.exeの引数として.ps1ファイルを指定する必要がありますが、.exeでファイルであれば普通にショートカットを作成することが出来ます。タスクスケジューラーなどに登録する場合も少し楽が出来そうです。

また、exeファイルですのでパスを切ってあげれば、拡張子無しのファイル名のみで実行することが可能です。ユーザーフォルダにbinフォルダを作成し$profileで$Env:Pathに追加します。作成したexeファイルをbinフォルダに移動してあげると、コンソールでの使い勝手が向上すると思われます。

よく使うps1ファイルををpsm1としてモジュール化していましたが、そういったスクリプトを.exe化すると良さそうです。


ps1もパスが切られていれば、ファイル名のみで実行可能でした。

スポンサーリンク

コメント