Skip to content

Instantly share code, notes, and snippets.

@marcgeld
Created May 23, 2017 11:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcgeld/80b406faa8f274dfee5cec0e4d96bf1e to your computer and use it in GitHub Desktop.
Save marcgeld/80b406faa8f274dfee5cec0e4d96bf1e to your computer and use it in GitHub Desktop.
Compile and load in Powershell
[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)"
@graysuit
Copy link

graysuit commented Jan 9, 2020

Well done Sir,

Also make a compiler for exe files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment