在C#中运行PowerShell命令

11
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
            }

        }

它抛出了异常:

System.Management.Automation.CommandNotFoundException: The term 'add-spsolution' is not recognized as the name of a cmdlet, function, script file, or operable program.

有什么建议吗?


你找到任何解决方案了吗? - love thakker
4个回答

11

首先添加以下命令:

Add-PSSnapin Microsoft.SharePoint.Powershell -EA 0


8

2

我最近遇到了这个问题。在我的情况下,我既不能看到添加的解决方案,也不能添加解决方案。因此,首先我使用以下 PowerShell 命令删除了解决方案:

(Get-SPSolution -Identity "YourSolution.wsp").Delete()

然后我成功添加了我的新代码解决方案。


-1

同时确保您从正在运行在IIS上的Web应用程序中运行"Add-SPSolution"命令,而不是使用标准的Visual Studio服务器(当您按F5时)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接