Created
November 28, 2018 15:33
-
-
Save KillerGoldFisch/574ff7fb4a332747d7bb2e94a654f9dd to your computer and use it in GitHub Desktop.
T4 template for executing a command
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
<#@ assembly name="System.dll" #> | |
<#@ import namespace="System" #> | |
<#@ import namespace="System.Diagnostics" #> | |
<#@ import namespace="System.Runtime.InteropServices" #> | |
<#@ import namespace="System.Text.RegularExpressions" #> | |
<#+ | |
public string Ex(string cmd) { | |
var escapedArgs = cmd.Replace("\"", "\\\""); | |
var fileName = "/bin/bash"; | |
var arguments = $"-c \"{escapedArgs}\""; | |
if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { | |
fileName = "cmd.exe"; | |
arguments = $"/C \"{escapedArgs}\""; | |
} | |
var process = new Process() | |
{ | |
StartInfo = new ProcessStartInfo | |
{ | |
FileName = fileName, | |
Arguments = arguments, | |
RedirectStandardOutput = true, | |
UseShellExecute = false, | |
CreateNoWindow = true, | |
} | |
}; | |
process.Start(); | |
string result = process.StandardOutput.ReadToEnd(); | |
process.WaitForExit(); | |
return result.Trim(); | |
} | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment