Please refer the original post from here http://blogs.msdn.com/b/kebab/archive/2014/04/28/executing-powershell-scripts-from-c.aspx I n today’s post, I will demonstrate the basics of how to execute PowerShell scripts and code from within a C#/.NET applications. I will walk through how to setup your project prerequisites, populate the pipeline with script code and parameters, perform synchronous and asynchronous execution, capture output, and leverage shared namespaces. Update 8/7/2014 : Here is the downloadable solution file. Update 11/5/2014 : Added a section on execution policy behavior. Prerequisites: First, ensure that PowerShell 2.0 or later is installed on the system you are developing on. The features used below will not be supported on PowerShell 1.0. Next, start by making a new console application (targeting .NET 4.0) in Visual Studio. In the solution explorer, add a project reference to the System.Management.Automation assembly * . On my machine (PowerShe...
Comments
Post a Comment