Skip to content

Instantly share code, notes, and snippets.

@dfinke
Last active February 26, 2023 11:57
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfinke/5148895 to your computer and use it in GitHub Desktop.
Save dfinke/5148895 to your computer and use it in GitHub Desktop.
function Invoke-Csx {
param($script = 'System.Console.WriteLine("Hello World");')
foreach($dll in (dir .\lib)) { Add-Type -Path $dll.FullName}
$engine = New-Object Roslyn.Scripting.CSharp.ScriptEngine $null, $null
$engine.AddReference("System")
$engine.AddReference("System.Core")
$session = $engine.CreateSession()
if(Test-Path $script -ErrorAction SilentlyContinue) {
$script = [System.IO.File]::ReadAllText( (Resolve-Path $script) )
}
$session.Execute($script)
}
PS C:\> Invoke-Csx @"
using System.Linq;
Enumerable.Range(1, 10).Select(x => x * x);
"@ | Where {$_ % 2 -eq 0} | ForEach {$_*2}
# Results
8
32
72
128
200
@glennblock
Copy link

Nice!

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