Подтвердить что ты не робот

Запускать команды 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
            }

        }

Выбрасывает исключение:

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

Любые предложения, почему?

4b9b3361

Ответ 1

Добавьте эту команду сначала:

Add-PSSnapin Microsoft.SharePoint.Powershell -EA 0

Ответ 3

У меня эта проблема недавно. В моем случае я не смог увидеть добавленное решение и не смог добавить решение. Поэтому сначала я удаляю решение, используя ниже команду PowerShell:

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

Затем мне удалось добавить новое решение для кода.

Ответ 4

Также убедитесь, что вы запускаете команду "Add-SPSolution" из веб-приложений, которая работает в IIS, и НЕ со стандартным сервером Visual Studio (когда вы нажимаете F5).