2012-07-12 58 views
0

我试图通过将它们嵌入到C#代码中来运行PowerShell脚本来激活Sharepoint站点上的(.wsp)模板功能。 我的代码如下在c#中的嵌入式PowerShell脚本Install-SPUserSolution没有识别

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. 我曾尝试将整个命令作为一个剧本,但我得到了 同样的错误。
  2. 我已经尝试了SharePoint管理shell 命令提示符下的脚本,它在那里完美工作。
  3. 我在WCF的库类中使用上述 代码。
  4. 我在Visual Studio中的每个 子项目的所有平台都设置为“ANY CPU”,并且我使用的是 .net framework 3.5。
  5. 我还检查了管理shell中的帮助选项 ,并且命令“Install-SPUserSolution”确实是 作为Microsoft.Sharepoint.Powershell的cmdlet存在。
  6. 我正在使用 客户端对象模型(COM)来完成我的任务,因此不能使用C# 代码来激活功能,因为COM不支持它。

我完全不知道我可能会做错什么。 任何帮助将不胜感激。

回答

0

我终于找到了解决我的问题, 因为我是试图在WCF运行这段代码,我所要做的就是 的SPADMIN帐户添加到我的应用程序池,那里的WCF托管, 使用sp.elevatepriviledges代理 和转换模拟