2012-07-12 33 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代理 和轉換模擬