Convert multiple PowerShell scripts into an Exe file

萝らか妹 提交于 2020-05-13 14:36:08

问题


I have few powershell script in orders. Can I convert the powershell scripts into one exe file ? I need set up it to run in order. Example, once installing with first script, second script need be installed and followed by 3,4,5 scripts.


回答1:


In the simplest case, you can use the following approach to merge your scripts into a single script that you can then package as an executable:

$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'

Note that this is a simplistic, but extensible approach: As written, there is no support for parameters, and no error handling: the respective contents of the original scripts are simply executed (&) in sequence, as script blocks ({ ... }).

You can compile the combined script, combined.ps1 to an executable, say, combined.exe, as follows, using the PS2EXE-GUI project's ps2exe.ps1 script (an updated and more fully featured version of the popular original, but obsolescent PS2EXE project).

# 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

Caveat: Generally, running the resulting executable requires the executing machine to have PowerShell installed, but due to targeting -runtime20 - in an effort to be v2-compatible - the .NET Framework 2.0 CLR must also be installed (note that it also comes with .NET Framework 3.5), which is no longer true by default in recent versions of Windows.



来源:https://stackoverflow.com/questions/59285526/convert-multiple-powershell-scripts-into-an-exe-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!