Skip to content

Instantly share code, notes, and snippets.

@KillerGoldFisch
Created November 28, 2018 15:33
Show Gist options
  • Save KillerGoldFisch/574ff7fb4a332747d7bb2e94a654f9dd to your computer and use it in GitHub Desktop.
Save KillerGoldFisch/574ff7fb4a332747d7bb2e94a654f9dd to your computer and use it in GitHub Desktop.
T4 template for executing a command
<#@ 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