【问题标题】:Embed SharePoint PowerShell with a C# Web Application使用 C# Web 应用程序嵌入 SharePoint PowerShell
【发布时间】:2012-08-21 13:29:33
【问题描述】:

我在 3.5 框架中运行一个 C# Web 应用程序,目标是 Any CPU。 我的机器配置: 带有 64 位操作系统的 Windows 7 视觉工作室 2010 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;
    } 
}

}

并得到以下错误。

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

请告诉我,我哪里做错了

谢谢, 桑迪普

【问题讨论】:

  • 为什么要调用 powershell 脚本,您已经准备好使用具有相关权限的托管代码直接访问 SharePoint OM?

标签: c# .net sharepoint


【解决方案1】:

您是否在运行此代码的 Web 服务器上安装了 sharepoint?错误消息非常简单。

【讨论】:

  • 是的,我在运行此 Web 应用程序的计算机上安装了 SharePoint 2010。 SharePoint 2010 和 VS 2010 都安装在同一台机器上。
【解决方案2】:

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

【讨论】:

  • 感谢 Rahil 的想法,我已经按照你说的那样做了。但问题出在其他地方。 VS 2010 适用于 32 位,SharePoint Powershell 适用于 64 位。因此,每当我试图从我的 Web 应用程序中调用 powershell 时,它都指向 32 位 powershell,这是错误的。所以我给出了 64 位 powershell 的路径,它可以工作。下面是工作代码
  • 请为其他访问者发布您的代码。还要标记答案和投票。谢谢
【解决方案3】:

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

我已经从批处理文件中调用了 Powershell 脚本,而我的批处理文件正在调用 64 位 poweshell。

在我写的批处理文件中 %windir%\sysnative\windowspowershell\v1.0\powershell -File "C:\filepath\Poweshellfile.ps1" auto

现在应用程序运行良好。

桑迪普·蒂瓦里 MCTS SharePoint 2010

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-06
    • 1970-01-01
    相关资源
    最近更新 更多