Skip to content

Instantly share code, notes, and snippets.

@nootn
Created February 5, 2013 11:28
Show Gist options
  • Save nootn/4713862 to your computer and use it in GitHub Desktop.
Save nootn/4713862 to your computer and use it in GitHub Desktop.
Run Powershell from .NET avoiding System.Management.Automation #4
// /*
// Copyright (c) 2013 Andrew Newton (http://www.nootn.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// */
using System.Collections.ObjectModel;
using System.Management.Automation; //see http://stackoverflow.com/a/1187978/281177 for where to reference this from
using System.Management.Automation.Runspaces;
using System.Text;
namespace PowershellTest
{
internal class RunPsUsingSystemManagementAutomation
{
public bool RunScript(string path, out string standardOutput, out string errorOutput)
{
errorOutput = "";
var scriptOutput = new StringBuilder();
var config = RunspaceConfiguration.Create();
//create Powershell runspace
using (var runspace = RunspaceFactory.CreateRunspace(config))
{
runspace.Open();
var results = new Collection<PSObject>();
//create a pipeline and feed it the script text
using (var pipeline = runspace.CreatePipeline())
{
//create the command to run
var cmd = new Command(path, true);
pipeline.Commands.Add(cmd);
//execute the script
try
{
pipeline.Invoke();
// Check for errors in the pipeline and throw an exception if necessary.
if (pipeline.Error != null && pipeline.Error.Count > 0)
{
var pipelineError = new StringBuilder();
pipelineError.AppendFormat("Pipeline Errors from Powershell Script: ");
foreach (var item in pipeline.Error.ReadToEnd())
{
pipelineError.AppendFormat("{0}\n", item);
}
errorOutput = pipelineError.ToString();
}
}
catch (System.Exception ex)
{
errorOutput = ex.ToString();
}
}
//close the runspace
runspace.Close();
//convert the script result into a single string
foreach (var obj in results)
{
scriptOutput.AppendLine(obj.ToString());
}
standardOutput = scriptOutput.ToString();
return string.IsNullOrWhiteSpace(errorOutput);
}
}
}
}
@timothy-witham
Copy link

Nice snibit. To make script output powershell script output, think line;
pipeline.Invoke();
should be
results = pipeline.Invoke();
😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment