Created
January 15, 2021 04:30
-
-
Save cawoodm/ef12815722c54384223160cc6c77d818 to your computer and use it in GitHub Desktop.
Run Any Code from PowerShell with Piston
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
<# | |
.Synopsis | |
Run any code via the piston API | |
.Description | |
Pass in your code and it will be run on a remote machine via the piston API. | |
See https://github.com/engineer-man/piston for more details | |
.Parameter Language | |
The name of the language: | |
awk, bash, brainfuck, c, cpp, csharp, deno, erlang, elixir, emacs, elisp, go, haskell, java, jelly, julia, kotlin, lua, nasm, node, paradoc, perl, php, python2, python3, ruby, rust, swift, typescript | |
.Parameter Code | |
The code block to run. This can be any powershell code which returns a string to run. | |
It can also just be the native code to run when used with the -Direct parameter (see examples) | |
.Example | |
./run.ps1 node {"console.log(1+1)"} | |
Run the code "console.log(1+1)" in node | |
.Example | |
./run.ps1 node {console.log(1+1)} -Direct | |
Run console.log in node directly | |
#> | |
[CmdletBinding()]param( | |
[Parameter(Mandatory = $true)][String][ValidateSet("awk", "bash", "brainfuck", "c", "cpp", "csharp", "deno", "erlang", "elixir", "emacs", "elisp", "go", "haskell", "java", "jelly", "julia", "kotlin", "lua", "nasm", "node", "paradoc", "perl", "php", "python2", "python3", "ruby", "rust", "swift", "typescript")] | |
$Language = "bash", | |
[scriptblock]$Code, | |
[string[]]$Arguments, | |
[switch]$Direct, | |
[string]$File | |
) | |
function main() { | |
Write-Verbose "Language: $Language" | |
if ($File) { | |
$SourceCode = [string](Get-Content $File -Raw) | |
} elseif ($Direct) { | |
$SourceCode = $Code.ToString() | |
} else { | |
$SourceCode = & $Code | |
} | |
Write-Verbose "Code:`n$SourceCode" | |
$req = [PSCustomObject]@{language = $Language; source = $SourceCode; args = @()}; | |
$Body = $req | ConvertTo-Json | |
Write-Verbose $Body | |
$hdr = @{"Content-Type" = "application/json"}; | |
$uri = "https://emkc.org/api/v1/piston/execute" | |
#$Proxy = "http://localhost:8888" | |
$result = Invoke-restMethod -Uri $uri -Body $Body -Headers $hdr -Method Post -Proxy $Proxy | |
if ($result.ran) { | |
Write-Verbose "Ran successfully:" | |
if ($result.stderr) { | |
$Host.UI.WriteErrorLine($result.stderr) | |
$result.stdout | |
} else { | |
$result.output | |
} | |
} else { | |
Write-Warning "Failed to run!" | |
$Host.UI.WriteErrorLine($result.stderr) | |
$result.stdout | |
} | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment