-
Notifications
You must be signed in to change notification settings - Fork 0
/
csc.ps1
31 lines (30 loc) · 1.54 KB
/
csc.ps1
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
param(
[Parameter(ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]$csharpCode,
$OutFile
)
#Ref: https://blog.csdn.net/eva001206130/article/details/79679883
# $providerDict
$providerDict = New-Object 'System.Collections.Generic.Dictionary[[string],[string]]'
$providerDict.Add('CompilerVersion','v4.0')
$codeCompiler = [Microsoft.CSharp.CSharpCodeProvider]$providerDict
Add-Type -AssemblyName System.IO.Compression
# 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 $pwd $OutFile
$compilerParameters.OutputAssembly = $outputExe
$compilerParameters.ReferencedAssemblies.Add( [System.Diagnostics.Process].Assembly.Location ) > $null
$compilerParameters.ReferencedAssemblies.Add("System.IO.compression.dll") > $null
$compilerParameters.ReferencedAssemblies.Add("System.Linq.dll") > $null
$compilerParameters.ReferencedAssemblies.Add("System.Xml.dll") > $null
$compilerParameters.ReferencedAssemblies.Add("System.Xml.Linq.dll") > $null
$compilerParameters.ReferencedAssemblies.Add("System.Collections.dll") > $null
$compilerParameters.ReferencedAssemblies.Add("System.IO.Compression.FileSystem.dll") > $null
# Compile Assembly
$compilerResult = $codeCompiler.CompileAssemblyFromSource($compilerParameters,$csharpCode)
Write-Output $compilerResult.Errors