public static string RunCommand(string command, IDictionary dictionary)
{
// create Powershell runspace
Runspace runspace = RunspaceFactory.CreateRunspace();
// open it
runspace.Open();
var cmd = new Command(command);
foreach (var pair in dictionary)
{
cmd.Parameters.Add(pair.Key, pair.Value);
}
// create a pipeline and feed it the command
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.Add(cmd);
Collection results = pipeline.Invoke();
// close the runspace
runspace.Close();
// convert the command result into a single string
var stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
return stringBuilder.ToString();
}
|