A buddy of mine at Microsoft was good enough to point me to this video on channel 9 that talks about using PowerShell from your C# code without doing the dreaded process and spawn routine we've probably all used before. To begin with, watch the 4 minute video at http://channel9.msdn.com/posts/bruceky/How-to-Embedding-PowerShell-Within-a-C-Application/. Once you get the basic steps there for adding the appropriate references to your code and using statements to your class, there are a few other useful things to know as it relates to SharePoint 2010 and PowerShell:
string pCmd = "New-SPExcelDataConnectionLibrary " + "-address 'http://farm2/sites/sporeports/dcl' " + "-ExcelServiceApplication 'Excel Services Application' ";
Finally, to execute your PowerShell script: ps.Invoke<string>();
Overall this is a pretty nice way to tackle scenarios when you have managed code but need to fallback to PowerShell for getting a task done. When you get into it there are some other interesting ways you can tackle using cmdlets, using the CmdletInfo, CommandMetadata and ProxyCommand classes. Unfortunately they still break down when you are working with non-public APIs but the basic approach I described above continues to work well in those cases too.
Note: Specific to using SP commands. Project must be compiled to target x64 platform and built with .Net 2.0.
Also if thinking about using in a install: CustomActions execute in a 32 bit process without post build changes to the msi via Orca.
How come when i do this it doesnt return anything
PowerShell ps = PowerShell.Create();
foreach (string s in ps.Invoke<string>())
it doesnt return anything the commands wont execute i want to add-spsoluiton , it doesnt add solution no error can any one help on this
Can this be done using .NET 3.5, PowerShell v2 and SharePoint 2007 in a 32-bit environment?
I am using this code block in timer job getting error. !!!! let me the procedure to call the same script in sharepoint timer job.