Note that there are some explanatory texts on larger screens.

plurals
  1. POEmbed SharePoint PowerShell with a C# Web Application
    primarykey
    data
    text
    <p>I am running a C# web application in 3.5 framework with Any CPU as target. My Machine Config: Windows 7 with 64bit OS Visual Studio 2010 SharePoint 2010</p> <p>I am trying to call a SharePoint PowerShell script from my web application. But failed. Below is my code.</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Management.Automation; using System.Management.Automation.Runspaces; using System.IO; using System.Collections.ObjectModel; using System.Text; namespace WebApplication3 { public partial class test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { try { string scriptText = ReadPowerShellScript("D:\\SiteHierarchy_project\\scriptSC.ps1"); RunspaceConfiguration rsConfig = RunspaceConfiguration.Create(); PSSnapInException snapInException = null; Runspace runspace = RunspaceFactory.CreateRunspace(); runspace.Open(); PSSnapInInfo info = rsConfig.AddPSSnapIn("Microsoft.SharePoint.Powershell", out snapInException); Pipeline pipeline = runspace.CreatePipeline(); string scriptSnapIn = "Get-PsSnapin -Registered"; pipeline.Commands.AddScript(scriptSnapIn); Runspace RunSpace = RunspaceFactory.CreateRunspace(rsConfig); RunSpace.Open(); Pipeline pipeLine = RunSpace.CreatePipeline(); Command scriptCommand = new Command(scriptText); pipeLine.Commands.AddScript(scriptText); // execute the script // Collection&lt;PSObject&gt; commandResults = pipeLine.Invoke(); pipeLine.Invoke(); // close the runspace RunSpace.Close(); } catch (Exception ex) { } } public string ReadPowerShellScript(string Script) { //Read script //StreamReader objReader = new StreamReader(Server.MapPath(Script)); StreamReader objReader = new StreamReader(Script); string strContent = objReader.ReadToEnd(); objReader.Close(); return strContent; } } </code></pre> <p>} </p> <p>and getting the below error.</p> <p>"The Windows PowerShell snap-in 'Microsoft.SharePoint.Powershell' is not installed on this machine."</p> <p>Please let me know , where i am doing wrong</p> <p>Thanks , Sandeep </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload