【发布时间】:2020-04-04 17:16:30
【问题描述】:
我的订单中几乎没有 powershell 脚本。 我可以将 powershell 脚本转换为一个 exe 文件吗?我需要设置它以按顺序运行。例如,使用第一个脚本安装后,需要安装第二个脚本,然后是 3,4,5 个脚本。
【问题讨论】:
标签: powershell powershell-2.0 azure-powershell
我的订单中几乎没有 powershell 脚本。 我可以将 powershell 脚本转换为一个 exe 文件吗?我需要设置它以按顺序运行。例如,使用第一个脚本安装后,需要安装第二个脚本,然后是 3,4,5 个脚本。
【问题讨论】:
标签: powershell powershell-2.0 azure-powershell
在最简单的情况下,您可以使用以下方法将脚本合并为单个脚本,然后您可以将其打包为可执行文件:
$scripts = 'script1.ps1', 'script2.ps1', 'script3.ps1'
(Get-Item $scripts | ForEach-Object {
"& {{`n{0}`n}}" -f (Get-Content -Raw $_.FullName)
}) -join "`n`n" > 'combined.ps1'
请注意,这是一种简单但可扩展的方法:正如所写,不支持参数,也不支持错误处理:原始脚本的相应内容只是按顺序执行(&),作为脚本块({ ... })。
您可以将组合脚本combined.ps1 编译为可执行文件,例如combined.exe,如下所示,使用PS2EXE-GUI 项目的ps2exe.ps1 脚本(更新且功能更全面流行的原始版本,但已过时 PS2EXE 项目)。
# Create a PSv2-compatible executable.
# Omit -runtime20 to create an executable for the same PowerShell version
# that runs the script.
ps2exe -inputFile combined.ps1 -outputFile combined.exe -runtime20
警告:通常,运行生成的可执行文件需要执行机器安装 PowerShell,但由于目标为 -runtime20 - 为了与 v2 兼容- 还必须安装 .NET Framework 2.0 CLR(请注意,它还附带 .NET Framework 3.5),在最新版本的 Windows 中不再默认安装.
【讨论】: