[Sample of Mar 3rd] Call PowerShell script from .NET - Microsoft All-In-One Code Framework - Site Home - MSDN Blogs
Microsoft All-In-One Code Framework - Developers' Pain Killer

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

Rate This
  • Comments 1

 

Homepage image
Sample of the Day RSS Feed

Sample download
C# version:
http://code.msdn.microsoft.com/CSPowerShell-61b48efd
VB version: http://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 http://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: http://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  http://msdn.microsoft.com/en-us/library/system.management.automation.runspaces.runspace(VS.85).aspx

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

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

Leave a Comment
  • Please add 6 and 4 and type the answer here:
  • Post
  • Good

Page 1 of 1 (1 items)