How to Embed PowerShell Within a C# Application

April 8, 2010

in C#,PowerShell

Check out James Brundage demoing this in this 4 minute Channel 9 video.

I took his example and worked up an extension method in C# to run PowerShell.

image

image

{ 1 trackback }

Tweets that mention How to Embed PowerShell Within a C# Application -- Topsy.com
04.08.10 at 8:50 pm

{ 10 comments… read them below or add one }

Daniel Chait 04.08.10 at 9:03 pm

whoa

Grant Steinfeld 04.10.10 at 11:09 am

where does one get the bits for the System.Management.Automation reference? I could not find it on my standard dev box?

Doug Finke 04.10.10 at 1:37 pm

Yon need to open the project file as a text file and add the following line into the

VS 2008/2010 do not provide it in the add reference even though it is in the GAC

Stephan Onisick 04.11.10 at 4:00 pm

Simply awesome!!

I’m assuming we could make a pipeline with other .AddCommand

Could something like this be used in InfoPath Managed Code to leverage the power of PowerShell.

Please look at my blog on infopathpainmanagement.com

Awesome!!

Doug Finke 04.11.10 at 8:51 pm

@Stephan, yes you can use the other methods and interfaces in the Management Automation namespace to create more sophisticated solutions.
I’ll take a look a your InfoPath site too.

Anthony 04.14.10 at 2:27 pm

Really great! Thanks for sharing!!!

Chris Mangiapane 03.08.12 at 8:25 pm

Thanks for the post!

I hoping there is a simple explaination. I created an exact copy of this code in a console application. Compiled in either 32/64 bit and the Get-Procces cmdlet gets invoked, but the result is always an empty string. I have tried several different scripts and all do the same thing, return and empty string. Please.

Doug Finke 03.09.12 at 10:26 am

Thanks for the comment Chris.

Email me or post the code (on StackOverflow) and we can take a look at it.

finked at hotmail
Doug

mohit 05.16.12 at 10:28 am

i am trying to invoke dtexec (sql server integration services package from a powershell script which is invoked through a c# application) and i get the error

commandnotfoundeception : ‘dtexec’ file not a command or ….

but if i go to the path where this file is located then i am able to run the package fine. is there any way that i can switch all the administrator environment variables with any crap where i have to mention all the paths.

Nandini 07.18.12 at 12:43 am

Hi James,
I am trying to do something similar to what u have done,except i am implementing it in a Library class tobe used by a WCF .Also the command I want to run is Install-SPUserSolution,But i get an error which says Install-SPUserSolution is not recognised as a name of a cmdlet,function..etc.If i run the same from the powershell management shell it runs fine..Do u have any idea what i might be doing missing ..below is a excerpt of my code.

Command installCommand = new Command(“Install-SPUserSolution”);
installCommand.Parameters.Add(“identity”, “Template.wsp”);
installCommand .Parameters .Add (“site”, “siteURL”);
// create Powershell runspace
Runspace runspace = RunspaceFactory.CreateRunspace();

// open it
runspace.Open()

// create a pipeline and feed it the script text
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(” Add-PsSnapin Microsoft.SharePoint.PowerShell”);

pipeline.Commands.Add(installCommand);
//added just for reference since command dosen’t return anything
pipeline.Commands.Add(“Out-String”);

// execute the script
Collection results = pipeline.Invoke();
(This part of the code throws error saying Install-SPUserSolution is not a recognized cmdlet,function,script,…)

// close the runspace
runspace.Close();

1)I have tried adding the whole command as a script, but I get the same error.
2)I have tried the script in SharePoint management shell command prompt and it works perfectly there.
3)I am using the above code in a Library Class of a WCF.
4)All my platforms of every sub-Project in Visual Studio is set to “ANY CPU” and my .NET framework is 3.5.
5)I have also checked the help option available in management shell and the command “Install-SPUserSolution” indeed exists as a cmdlet of Microsoft.Sharepoint.Powershell.
6)I am using Client Object Model(COM) to complete my task, hence cannot use C# code to activate feature since COM doesn’t support it
I am completely clueless as to what I might be doing wrong.
Any help would be greatly appreciated.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Contrat Creative Commons

© 2007-2013, Doug Finke