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.
Researching the optimal; implementing the practical
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.

© 2007-2013, Doug Finke
{ 1 trackback }
{ 10 comments… read them below or add one }
whoa
where does one get the bits for the System.Management.Automation reference? I could not find it on my standard dev box?
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
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!!
@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.
Really great! Thanks for sharing!!!
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.
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
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.
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.