运行PowerShell命令
问题描述:
RunspaceConfiguration psConfig = RunspaceConfiguration.Create();
Runspace psRunspace = RunspaceFactory.CreateRunspace(psConfig);
psRunspace.Open();
using (Pipeline psPipeline = psRunspace.CreatePipeline())
{
// Define the command to be executed in this pipeline
Command command = new Command("Add-spsolution");
// Add a parameter to this command
command.Parameters.Add("literalpath", @"c:\project3.wsp");
// Add this command to the pipeline
psPipeline.Commands.Add(command);
// Invoke the cmdlet
try
{
Collection<PSObject> results = psPipeline.Invoke();
Label1.Text = "hi"+results.ToString();
// Process the results
}
catch (Exception exception)
{
Label1.Text = exception.ToString();// Process the exception here
}
}
它抛出异常:运行PowerShell命令
System.Management.Automation.CommandNotFoundException: The term 'add-spsolution' is not recognized as the name of a cmdlet, function, script file, or operable program.
任何建议,为什么?
答
您必须使用import-module命令为sharepoint加载正确的模块。使用get-module来查找可用的模块。
要做到这一点编程,看到关于这个问题我的帖子:
-Oisin
答
添加该命令先:
Add-PSSnapin Microsoft.SharePoint.Powershell -EA 0
答
另外,还要确保你从运行在IIS上的Web应用程序运行“Add-SPSolution”命令,而不是使用标准的Visual Studio se运行rver(当你按下F5时)。
答
我最近有这个问题。就我而言,我无法看到增加的解决方案,也无法添加解决方案。所以首先我使用下面的PowerShell命令删除解决方案:
(Get-SPSolution -Identity "YourSolution.wsp").Delete()
然后我能够添加我的新代码解决方案。
您是否找到任何解决方案? – 2016-03-31 07:14:53