Skip to content

Instantly share code, notes, and snippets.

@nootn
Created February 5, 2013 11:51
Show Gist options
  • Save nootn/4713988 to your computer and use it in GitHub Desktop.
Save nootn/4713988 to your computer and use it in GitHub Desktop.
Run Powershell from .NET avoiding System.Management.Automation #5
// /*
// 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;
using System.Diagnostics;
using System.IO;
namespace TestPowerShell
{
internal class Program
{
private static readonly string PowerShellRunnerBatchFilePath = Path.Combine(
Environment.CurrentDirectory, "PowerShellRunner.bat"); //change to which ever batch file you want to run
private static readonly string PowerShellRunnerTestPowershellFilePath = Path.Combine(
Environment.CurrentDirectory, "TestPowerShell.ps1"); //change to which ever powershell file you want to run
private static void Main(string[] args)
{
string output;
string errorOutput;
if (RunScript(PowerShellRunnerTestPowershellFilePath, out output, out errorOutput))
{
Console.WriteLine("Successfully ran script with no errors!");
Console.WriteLine("Output:");
Console.WriteLine(output);
}
else
{
Console.WriteLine("Failed to run script");
Console.WriteLine("Error Output:");
Console.WriteLine(errorOutput);
Console.WriteLine("Output:");
Console.WriteLine(output);
}
//TODO: remove if running in background
Console.WriteLine("Press 'Enter' to exit...");
Console.ReadLine();
}
public static bool RunScript(string path, out string standardOutput, out string errorOutput)
{
var proc = new Process
{
StartInfo =
{
FileName = PowerShellRunnerBatchFilePath,
Arguments = path,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
}
};
proc.Start();
standardOutput = proc.StandardOutput.ReadToEnd();
errorOutput = proc.StandardError.ReadToEnd();
return string.IsNullOrWhiteSpace(errorOutput);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment