[Sample of Mar 3rd] Call PowerShell script from .NET

 

Homepage image
Sample of the Day RSS Feed

Sample download
C# version:
https://code.msdn.microsoft.com/CSPowerShell-61b48efd
VB version: https://code.msdn.microsoft.com/VBPowerShell-6b4f83ea

Today’s code sample indicates how to call PowerShell script from C# and VB.NET code. It first creates a Runspace object in System.Management.Automation namespace. Then it creates a Pipeline from Runspace. The Pipeline is used to host a line of commands which are supposed to be executed. The example call Get-Process command to get all processes whose name start with "V".  If your .NET project needs to interact with PowerShell script, this code sample could be helpful to you.

imageYou can find more code samples that demonstrate the most typical programming scenarios by using Microsoft All-In-One Code Framework Sample Browser or Sample Browser Visual Studio extension. They give you the flexibility to search samples, download samples on demand, manage the downloaded samples in a centralized place, and automatically be notified about sample updates. If it is the first time that you hear about Microsoft All-In-One Code Framework, please watch the introduction video on Microsoft Showcase, or read the introduction on our homepage https://1code.codeplex.com/.

 

Introduction

This code sample indicates how to call PowerShell script from C# and VB.NET code. It first creates a Runspace object in System.Management.Automation namespace. Then it creates a Pipeline from Runspace. The Pipeline is used to host a line of commands which are supposed to be executed. The example call Get-Process command to get all processes whose name start with "V". 

 

Building the Sample

To create this project, we first need to install PowerShell. We can find the download link from the following KB article: https://support.microsoft.com/kb/968929

 

Running the Sample

Press F5 to run this application, and you will see following result.

image

Using the Code

1. Create a RunSpace to host the Powershell script environment using RunspaceFactory.CreateRunSpace.

 Runspace runSpace = RunspaceFactory.CreateRunspace(); 
runSpace.Open(); 

2. Create a Pipeline to host commands to be executed using Runspace.CreatePipeline.

 Pipeline pipeLine = runSpace.CreatePipeline(); 

3. Create a Command object by passing the command to the constructor.

 Command getProcessCStarted = new Command("Get-Process"); 
// Add parameters to the Command.  
getProcessCStarted.Parameters.Add("name", "C*"); 

4. Add the commands to the Pipeline.

 pipeLine.Commands.Add(getProcessCStarted); 

5. Run all commands in the current pipeline by calling Pipeline.Invoke. It returns a System.Collections.ObjectModel.Collection object.  In this example, the executed script is "Get-Process -name C*".

 Collection<PSObject> cNameProcesses = pipeLine.Invoke(); 
 
foreach (PSObject psObject in cNameProcesses) 
{ 
    Process process = psObject.BaseObject as Process; 
    Console.WriteLine("Process Name: {0}", process.ProcessName); 
} 

 

More Information

MSDN: Runspace Class  https://msdn.microsoft.com/en-us/library/system.management.automation.runspaces.runspace(VS.85).aspx

MSDN: Pipeline.Invoke Method ()  https://msdn.microsoft.com/en-us/library/ms569128(VS.85).aspx

How to call Powershell Script function from C# ?  https://social.msdn.microsoft.com/Forums/en-AU/csharpgeneral/thread/faa70c95-6191-4f64-bb5a-5b67b8453237