能否从PowerShell脚本创建可执行的 file.exe
文件?
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,您的脚本可以被加密,因此它稍微安全一些以免被窥探……但这基本上只是混淆,并且在某些方面更糟糕,本质上和批处理文件没有什么区别。
是的,使用PS2EXE可以创建*.exe
文件。
整个过程非常简单,而且已经很好地解释了,但是这里有一段代码片段:
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
要添加和编辑版本信息,请使用类似VERPATCH的工具。
向名为PythonEXE的git仓库致敬。
它演示了如何从Python项目创建可执行文件,并提供了一个YouTube逐步指南。
使用PowerGUI的脚本编辑器(它是免费的并且可用)。在PowerGUI脚本编辑器中打开您的脚本 > 工具 > 编译脚本 > 选择您想要的任何选项以生成 .exe 文件(密码保护源代码,在 .exe 运行后自动关闭控制台等)。
开箱即用 - 不行。但我编写了一个PowerShell脚本,可以将脚本包装成EXE文件。我早些时候创建了这个脚本,但决定在这里博客分享给大家。
我理解你的问题是“我的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"
}
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
PrimalScript 是由Sapien开发的一款工具,可以将 PowerShell 脚本生成可执行文件。需要注意的是,运行生成的可执行文件所在的机器必须安装 PowerShell。