将SharePoint PowerShell嵌入C#Web应用程序

将SharePoint PowerShell嵌入C#Web应用程序,c#,.net,sharepoint,C#,.net,Sharepoint,我正在3.5框架下运行一个以任何CPU为目标的C#web应用程序。 我的机器配置: 带64位操作系统的Windows 7 VisualStudio2010 SharePoint 2010 我正在尝试从web应用程序调用SharePoint PowerShell脚本。但失败了。下面是我的代码 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI

我正在3.5框架下运行一个以任何CPU为目标的C#web应用程序。 我的机器配置: 带64位操作系统的Windows 7 VisualStudio2010 SharePoint 2010

我正在尝试从web应用程序调用SharePoint PowerShell脚本。但失败了。下面是我的代码

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<PSObject> 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;
    } 
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用系统、管理、自动化;
使用System.Management.Automation.Runspaces;
使用System.IO;
使用System.Collections.ObjectModel;
使用系统文本;
命名空间WebApplication3
{
公共部分类测试:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
尝试
{
字符串scriptText=ReadPowerShellScript(“D:\\SiteHierarchy\u项目\\scriptSC.ps1”);
RunspaceConfiguration rsConfig=RunspaceConfiguration.Create();
PSSnapInException snapInException=null;
Runspace Runspace=RunspaceFactory.CreateRunspace();
Open();
PSSnapInInfo info=rsConfig.AddPSSnapIn(“Microsoft.SharePoint.Powershell”,out snapInException);
Pipeline Pipeline=runspace.CreatePipeline();
字符串scriptSnapIn=“Get-PsSnapin-Registered”;
pipeline.Commands.AddScript(scriptSnapIn);
Runspace Runspace=RunspaceFactory.CreateRunspace(rsConfig);
Open();
Pipeline Pipeline=RunSpace.CreatePipeline();
命令scriptCommand=新命令(scriptText);
pipeLine.Commands.AddScript(scriptText);
//执行脚本
//Collection commandResults=pipeLine.Invoke();
pipeLine.Invoke();
//关闭运行空间
RunSpace.Close();
}
捕获(例外情况除外)
{ }
}
公共字符串ReadPowerShellScript(字符串脚本)
{
//读剧本
//StreamReader objReader=新的StreamReader(Server.MapPath(脚本));
StreamReader objReader=新的StreamReader(脚本);
字符串strContent=objReader.ReadToEnd();
objReader.Close();
返回strContent;
} 
}
}

并得到下面的错误

“此计算机上未安装Windows PowerShell管理单元‘Microsoft.SharePoint.PowerShell’。”

请让我知道,我哪里做错了

谢谢,
Sandeep

在运行此代码的web服务器上是否安装了sharepoint?错误信息非常直截了当

只是一个想法;
创建批处理文件以调用.ps1文件。您可以使用System.Diagnostics.ProcessStartInfo调用批处理文件,而不是直接从代码调用.ps1文件

解决方案是调用64位powershell,而不是仅在VS 2010中使用

我从批处理文件调用了Powershell脚本,批处理文件调用的是64位poweshell

在我的批处理文件中,我写了 %windir%\sysnative\windowspowershell\v1.0\powershell-文件“C:\filepath\Poweshellfile.ps1”自动

现在应用程序运行良好

桑代普·蒂瓦里
MCTS SharePoint 2010

为什么要调用powershell脚本,您已准备好使用托管代码并具有直接访问SharePoint OM的相关权限?是的,我在运行此web应用程序的计算机上安装了SharePoint 2010。SharePoint 2010和VS 2010都安装在同一台计算机上。感谢Rahil的想法,我已经按照您所说的方式完成了。但问题出在其他地方。VS 2010适用于32位,SharePoint Powershell适用于64位。因此,每当我试图从web应用程序中调用powershell时,它都指向32位powershell,这是错误的。所以我给出了64位powershell的路径,它是有效的。以下是工作代码,请为其他访客发布您的代码。同时标记答案和投票。谢谢