我正在以任何 CPU 为目标在 3.5 框架中运行 C# Web 应用程序。我的机器配置:带有 64 位操作系统的 Windows 7 Visual Studio 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’。”
请让我知道,我在哪里做错了
谢谢,桑迪普