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
119
120
121
|
#1、脚本
function
Convert-PS1ToExe
{
param
(
[
Parameter
(
Mandatory
=
$true
)]
[ValidateScript({
$true
})]
[ValidateNotNullOrEmpty()]
[IO.FileInfo]
$ScriptFile
)
if
(
-not
$ScriptFile
.Exists)
{
Write-Warning
"$ScriptFile not exits."
return
}
[string]
$csharpCode
=
@'
using System;
using System.IO;
using System.Reflection;
using System.Diagnostics;
namespace LoadXmlTestConsole
{
public class ConsoleWriter
{
private static void Proc_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
{
Process pro = sender as Process;
Console.WriteLine(e.Data);
}
static void Main(string[] args)
{
// Set title of console
Console.Title = "Powered by PSTips.Net";
// read script from resource
Assembly ase = Assembly.GetExecutingAssembly();
string scriptName = ase.GetManifestResourceNames()[0];
string scriptContent = string.Empty;
using (Stream stream = ase.GetManifestResourceStream(scriptName))
using (StreamReader reader = new StreamReader(stream))
{
scriptContent = reader.ReadToEnd();
}
string scriptFile = Environment.ExpandEnvironmentVariables(string.Format("%temp%\\{0}", scriptName));
try
{
// output script file to temp path
File.WriteAllText(scriptFile, scriptContent);
ProcessStartInfo proInfo = new ProcessStartInfo();
proInfo.FileName = "PowerShell.exe";
proInfo.CreateNoWindow = true;
proInfo.RedirectStandardOutput = true;
proInfo.UseShellExecute = false;
proInfo.Arguments = string.Format(" -File {0}",scriptFile);
var proc = Process.Start(proInfo);
proc.OutputDataReceived += Proc_OutputDataReceived;
proc.BeginOutputReadLine();
proc.WaitForExit();
Console.WriteLine("Hit any key to continue...");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine("Hit Exception: {0}", ex.Message);
}
finally
{
// delete temp file
if (File.Exists(scriptFile))
{
File.Delete(scriptFile);
}
}
}
}
}
'@
# $providerDict
$providerDict
=
New-Object
'System.Collections.Generic.Dictionary[[string],[string]]'
$providerDict
.Add(
'CompilerVersion'
,
'v4.0'
)
$codeCompiler
=
[Microsoft.CSharp.CSharpCodeProvider]
$providerDict
# Create the optional compiler parameters
$compilerParameters
=
New-Object
'System.CodeDom.Compiler.CompilerParameters'
$compilerParameters
.GenerateExecutable =
$true
$compilerParameters
.GenerateInMemory =
$true
$compilerParameters
.WarningLevel = 3
$compilerParameters
.TreatWarningsAsErrors =
$false
$compilerParameters
.CompilerOptions =
'/optimize'
$outputExe
=
Join-Path
$ScriptFile
.Directory
"$($ScriptFile.BaseName).exe"
$compilerParameters
.OutputAssembly =
$outputExe
$compilerParameters
.EmbeddedResources.Add(
$ScriptFile
.FullName) >
$null
$compilerParameters
.ReferencedAssemblies.Add(
[System.Diagnostics.Process]
.Assembly.Location ) >
$null
# Compile Assembly
$compilerResult
=
$codeCompiler
.CompileAssemblyFromSource(
$compilerParameters
,
$csharpCode
)
# Print compiler errors
if
(
$compilerResult
.Errors.HasErrors)
{
Write-Host
'Compile faield. See error message as below:'
-ForegroundColor Red
$compilerResult
.Errors |
foreach
{
Write-Warning
(
'{0},[{1},{2}],{3}'
-f
$_
.ErrorNumber,
$_
.Line,
$_
.Column,
$_
.ErrorText )
}
}
else
{
Write-Host
'Compile succeed.'
-ForegroundColor Green
"Output executable file to '$outputExe'"
}
}
2、输入
Convert-PS1ToExe
-ScriptFile .\test.ps1
|
本文转自bard_zhang51CTO博客,原文链接: http://blog.51cto.com/timefiles/1794539,如需转载请自行联系原作者