从PowerShell脚本创建可执行的.exe文件?

问题描述 投票:31回答:8

是否可以从PowerShell脚本创建可执行的file.exe文件?

powershell executable
8个回答
19
投票

没有。至少,还没有,在5个版本的PowerShell之后,它似乎不太可能。

我希望我可以放弃它,但其他人提供了一堆“解决方法”类型的答案,我觉得有必要解决:

您可以将.ps1脚本包装在另一个脚本类型中以使其可双击,甚至可以生成嵌入了脚本的可执行文件(请参阅此线程上的其他答案)......但这些“可执行文件”需要正确的版本PowerShell已经存在于系统中,所以你没有通过这样做获得任何东西,并且你失去了PowerShell的许多功能(如流对象输出,帮助文档和自动参数处理以及用户的选项卡完成) 。

这是一个简单的.cmd批处理文件包装器(您可以扩展它以允许参数):

REM <#
copy %0 %0.ps1
PowerShell.exe -ExecutionPolicy Unrestricted -NoProfile -Command "&{Set-Alias REM Write-Host; .\%0.ps1}"
del %0.ps1
exit
REM #>

### Your PowerShell script goes below here.
### I've put a couple of lines as an example ...
ls | sort length -desc | select -first 5 | ft
ps | sort ws -desc | select -first 10 | ft

我知道 ...

使用Portable PowerShell,可能会打包一种包含正确版本的PowerShell和脚本的自解压zip,并且可以正常工作。这不是任何正常意义上的可执行文件 - 这有点像Valve是否已经决定只将拇指驱动器上的vmware映像作为让Linux用户玩半条命的解决方案。但是,该产品似乎已被抛弃。

使用PrimalScript(或PowerShell Studio)或PowerGui或pShellExec,您的脚本可以加密,因此它可以防止窥探......但这基本上只是混淆,基本上与批处理文件没有什么不同,在某些方面更糟糕。


8
投票

开箱即用 - 没有。但是我已经构建了一个PowerShell脚本,可以获取脚本并在其周围创建一个EXE包装器。我不久前创建了这个脚本,但决定让blog it here让人们看看。


7
投票

是的,有一个PS2EXE选项来创建这样的*.exe文件。

Usage

整个过程非常简单,但很好地解释了这里是一个片段:

C:\Scripts\PS2EXE\PS2EXE_0.5.0.0.0\ps2exe.ps1 
    -inputFile  C:\Scripts\ExampleEXE\example.ps1 
    -outputFile C:\Scripts\ExampleEXE\example.exe -sta -noConsole -runtime40 -verbose -x64

唯一不好的是该项目已经折旧。自2015年以来没有更新或新版本。

编辑:这个预计已被接收,现在由一个新人维持。您可以在此处找到更新后的代码,上次更新时间为01/04/2018。 https://gallery.technet.microsoft.com/scriptcenter/PS2EXE-GUI-Convert-e7cb69d5


Version Information

要添加和编辑版本信息,请使用VERPATCH之类的内容。


更新2019年

喊出一个叫做PythonEXE的git-repo。

它演示了如何从Python项目创建可执行文件,还提供了YouTube循序渐进指南。


6
投票

使用PowerGUI's Script Editor(它是免费的并且有效)。在PowerGUI脚本编辑器>工具>编译脚本中打开脚本>选择您想要的.exe选项(密码保护源代码,在.exe运行后自动关闭控制台等)。


3
投票

我将您的问题理解为“我的PowerShell脚本可以生成可执行文件吗?”这就是我发现这篇文章时我想要做的事情。这可以使用Add-Type命令。

Add-Type -TypeDefinition @"
    using System;
    class Program {
        public static void Main(string[] args) { 
            Console.WriteLine("Hello World!");
        }
    }
"@ -CompilerParameters @{
    "GenerateExecutable" = $true
    "OutputAssembly" = "test2.exe"
}

2
投票

来自PrimalScriptSapien将从PowerShell脚本生成一个exe。运行可执行文件的机器必须安装PowerShell。


2
投票

我发现最好将exehell脚本作为exe分发的解决方案是将其包装在NSIS可执行文件中。

我写这样的.nsi脚本:

Name "Maintenance task"
OutFile "maintenance.exe"
ShowInstDetails show

Section "Main"
  ;Executes the "script-to-run.ps1" PowerShell script
  InitPluginsDir
  SetOutPath "$pluginsdir\MyOrg" ;It is better to put stuff in $pluginsdir, $temp is shared
  ;extract the .ps1 and run it, collecting output into details.
  File script-to-run.ps1
  nsExec::ExecToLog 'powershell  -inputformat none -ExecutionPolicy RemoteSigned -File "$pluginsdir\MyOrg\script-to-run.ps1"  '
  SetOutPath $exedir
SectionEnd

我只需要使用NSIS工具链将脚本编译为exe,它将在任何具有PowerShell的操作系统上运行,无论执行策略是什么。

这是受How to call PowerShell in NSIS这个问题的启发。


0
投票

有一个名为Portable PowerShell的项目,在几年后仍处于测试阶段......可能值得关注您的需求。

http://shelltools.wik.is/Portable_PowerShell

© www.soinside.com 2019 - 2024. All rights reserved.