I am trying to run powershell scripts by embedding them in C# code inorder to activate a (.wsp) template feature on a Sharepoint site. My Code is as follows
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<PSObject> 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 i am using .net framework 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.