Created
May 23, 2017 11:35
-
-
Save marcgeld/80b406faa8f274dfee5cec0e4d96bf1e to your computer and use it in GitHub Desktop.
Compile and load in Powershell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[string] $scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Definition | |
[string] $fileName = $($MyInvocation.MyCommand).ToString().Replace(".ps1", ".dll") | |
[string] $dllPath = Join-Path $scriptPath $fileName | |
Write-Host "Dll fileName: $($fileName)" | |
Write-Host "Dll scriptPath: $($scriptPath)" | |
Write-Host "Dll Path: $($dllPath)" | |
$compileCode = @" | |
using System; | |
namespace Ns | |
{ | |
public class Clazz | |
{ | |
public static void Main() { | |
Console.WriteLine("Hello there...!"); | |
} | |
} | |
} | |
"@ | |
$CompilerParameters = New-Object -TypeName System.CodeDom.Compiler.CompilerParameters | |
$CompilerParameters.CompilerOptions = '/debug-' | |
$CompilerParameters.OutputAssembly = $dllPath | |
Add-Type -CompilerParameters $CompilerParameters -TypeDefinition $compileCode | |
[Void][Reflection.Assembly]::LoadFile( $CompilerParameters.OutputAssembly ) | |
[Ns.Clazz]::Main() | |
Write-Host "$($MyInvocation.MyCommand)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Well done Sir,
Also make a compiler for exe files.