-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpowershell2exe
118 lines (82 loc) · 6.31 KB
/
powershell2exe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
https://gallery.technet.microsoft.com/PS2EXE-Convert-PowerShell-9e4e07f1
PS2EXE - v0.5
Release Notes:
v0.5
v0.4
v0.3
Original description of version 0.1 with updates - especially in the "Usage" section
In the last days I created the tool “PS2EXE”. It is able to “convert” PowerShell scripts to “standalone” EXE files.
But: It does not convert the PowerShell script to an other language! It encapsulates the script with a lightweight PowerShell host written in C# and compiles the dynamically generated C# source code in memory to an EXE file. The resulting EXE is an .NET assembly that contains the source script encoded in Base64. The EXE includes all stuff that is needed to execute an PowerShell through the .NET object model. It is based on classes in the namespace System.Management.Automation that reperents the PowerShell engine. – Therefore the EXE file is not a real “standalone” EXE file. It needs PowerShell to be installed!!! And – of course – it needs .NET Framework v2.0. Furthermore “script execution” have to be allowed (see cmdlet: set-execultionpolicy). – The resulting EXE is “MSIL” and is able to execute as x64 or x86.
The tool “PS2EXE” itself is a PowerShell script! – It does the in-memory compilation and generates the EXE file. It uses the CSharpCodeProvider class of namespace Microsoft.CSharp.
The script is really simple. I contains a multiline string that represents the PowerShell host I’ve written. This is much more interesting than the PS2EXE.ps1 script itself. – Have a look into it!
Usage:
Call the script with this parameters:
-inputFile PowerShell script file
-outputFile file name (with path) for the destination EXE file
-debug (switch) generate debug info in the destination EXE file. The dynamically generated .CS file will stored beside the output EXE file. Furthermore a .PDB file will be generated for the EXE file
-verbose (switch) shows also verbose informations – if any.
-x86 (switch) compile EXE to run as 32 bit application
-x64 (switch) compile EXE to run as 64 bit application
-runtime20 (switch) force running the EXE in PowerShell 2.0 using .NET 2.0
-runtime30 (switch) force running the EXE in PowerShell 3.0 using .NET 4.0
-lcid specify language ID for threads
-sta run PowerShell environment in Single Thread Apartment mode
-mta run PowerShell environment in Multithread Apartment mode
-noconsole compile PS script as Windows application
Sample:
image_thumb[2]
This creates “test.exe” out of the PowerShell source file “test.ps1”
Limitations: It’s not easy to create a fully functional PowerShell host such as “Console host” (powershell.exe) or “ISE” (powershell_ise.exe). So there may be functionality that does not work properly.
The generated EXE can also be calls using command line options. There are 4 options that are used by the PowerShell host:
-debug Forces the EXE to be debugged. It calls “System.Diagnostics.Debugger.Break()”.
-extract:”Filename” Extracts the PowerShell script inside the EXE and saves it as “Filename”. The script will not be executed.
-wait At the end of the script execution it writes “Press a key…” and waits for a key to be pressed.
-end All following options will be passed to the script inside the EXE. All preceding options are used by the EXE and will not be passed to the script.
Sample:
I create a script file containing this line of code:
PowerShell
Edit|Remove
$args | Write-Host
I save it as “c:\test2.ps1” and convert it as EXE by using PS2EXE:
image_thumb[5]
Sample 1.: “-wait” forces the “Hit any key…” message. All options following “-end” will be passed to the script.
Sample 2., 3. : The script will not get options preceding to “-end”.
Sample 4: “-wait” follows to “-end” and so it’s passed to the script. No message “Hit any key…”.
So. That’s it for the moment. Please feel free to modify the script and let me know.
Possible tasks:
Sign the script inside the EXE with code signature
Sign the EXE with code signature
Compress the script inside the EXE
Improve the PSHost implementation inside the EXE.
Have fun!
New in v0.5.0.0 (14/11/2013):
support for PowerShell 4.0
VS project updated to VS2013 and .NET 4.0
New in v0.4.0.0 (03/09/2013):
parameter -sta for running PowerShell in Single Thread Apartment mode
parameter -mta for running PowerShell in Multi Thread Apartment mode
parameter -noconsole to compile the resulting exe file als "Windows Application". the only implemented GUI interaction is for "get-credential"!!
Blog article coming soon...
New in v0.3.0.0 (03/08/2013):
Support for PowerShell 3.0 and 2.0
Bug fixes (especially "Compilation fails - Assembly not referenced")
see Blog Article: http://blog.karstein-consulting.com/2013/03/08/update-of-ps2exe-version-0-3-0-0-now-supports-powershell-3-0-and-2-0/
Here are the previous articles on my blog:
[v0.2.0.0] https://blog.karstein-consulting.com/2012/04/30/ps2exe-v0-2-0-0-improvements-platform-switch-x64-or-x86-or-anycpu-new-exe-config-file-for-supported-runtime/
[original] http://blog.karstein-consulting.com/2011/06/21/ps2exe-tool-for-converting-powershell-scripts-to-standalone-exe-files/
已在以下平台上进行验证
Windows 10 不能
Windows Server 2012 能
Windows Server 2012 R2 不能
Windows Server 2008 R2 能
Windows Server 2008 能
Windows Server 2003 不能
Windows Server 2016 不能
Windows 8 能
Windows 7 能
Windows Vista 不能
Windows XP 不能
Windows 2000 不能
作者已在这些平台上对此脚本进行测试。它可能也适用于其他平台。如果您试用后发现脚本适用于其他平台,请在脚本讨论中添加注释以告诉其他用户。
免责声明
示例脚本不在任何 Microsoft 标准支持计划或服务的支持范围内。示例脚本“按原样”提供,无任何类型的担保。Microsoft 也不提供任何暗示的担保,包括但不限于任何暗示的适销性或特定用途适用性的担保。因使用或执行示例脚本及文档所带来的全部风险由您自行承担。在任何情况下,对于因使用或无法使用示例脚本或文档资料而导致的任何损害(包括但不限于企业利润损失、业务中断、业务信息丢失及其他经济损失),Microsoft、其创作人员或与脚本的创建、产生及交付有关的任何人员均不承担责任,即使 Microsoft 已被告知这种损害可能性。