【问题标题】:Convert multiple PowerShell scripts into an Exe file将多个 PowerShell 脚本转换为一个 Exe 文件
【发布时间】:2020-04-04 17:16:30
【问题描述】:

我的订单中几乎没有 powershell 脚本。 我可以将 powershell 脚本转换为一个 exe 文件吗?我需要设置它以按顺序运行。例如,使用第一个脚本安装后,需要安装第二个脚本,然后是 3,4,5 个脚本。

【问题讨论】:

    标签: powershell powershell-2.0 azure-powershell


    【解决方案1】:

    在最简单的情况下,您可以使用以下方法将脚本合并为单个脚本,然后您可以将其打包为可执行文件:

    $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 中不再默认安装.

    【讨论】:

    • 可能值得在这里添加这个答案的一个版本,我认为你的答案如果更详细并且可能更好用 - stackoverflow.com/questions/48338017/…
    • 谢谢@Matthew,但这个答案太专门针对这个问题量身定做;概括它以使其有用作为链接问题的答案将需要更多的努力。
    • 谢谢@mklement0,我在这里提出了一个问题:stackoverflow.com/q/59993465/139212
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多