Created
September 25, 2016 20:19
-
-
Save Dan1el42/541e4132ddaaf53ce35e11b7b775473c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var script = System.IO.File.ReadAllText(textBoxScriptToRun.Text); | |
var match = Regex.Match(script, "function (?<FunctionName>[a-z]+-[a-z0-9]+)", | |
RegexOptions.IgnoreCase); | |
var functionName = match.Groups["FunctionName"]; | |
Hashtable functionParams = new Hashtable(); | |
foreach (var line in textBoxParameters.Lines) | |
{ | |
var tokens = line.Split("=".ToCharArray(), 2); | |
if (tokens.Length == 2) | |
{ | |
functionParams.Add(tokens[0], tokens[1]); | |
} | |
} | |
var scriptBlock = ScriptBlock.Create( | |
new StringBuilder() | |
.AppendLine("param ([Parameter(Mandatory)][Hashtable]$FunctionParams)") | |
.AppendLine(script) | |
.AppendLine(String.Format("{0} @FunctionParams", functionName)).ToString() | |
); | |
using (PowerShell ps = PowerShell.Create()) | |
{ | |
ps.AddCommand("Invoke-Command") | |
.AddParameter("ComputerName", textBoxRemotePCName.Text) | |
.AddParameter("ScriptBlock", scriptBlock) | |
.AddParameter("ArgumentList", functionParams); | |
var results = ps.Invoke(); | |
StringBuilder sb = new StringBuilder(); | |
if (ps.HadErrors) | |
{ | |
foreach (var error in ps.Streams.Error.ReadAll()) | |
{ | |
sb.AppendLine(error.ToString()); | |
} | |
} | |
foreach (var result in results) | |
{ | |
sb.AppendLine(result.ToString()); | |
} | |
textBoxOutput.Text = sb.ToString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment