Ausführen von powershell-Befehlen in C#
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
}
}
Ist es das werfen der Ausnahme:
System.Management.Automation.CommandNotFoundException: The term 'add-spsolution' is not recognized as the name of a cmdlet, function, script file, or operable program.
Irgendwelche Vorschläge warum?
- hast du eine Lösung?
Du musst angemeldet sein, um einen Kommentar abzugeben.
Hinzufügen mit diesem Befehl zuerst:
Add-PSSnapin Microsoft.SharePoint.Powershell -EA 0
Müssen Sie den import-module-Befehl laden Sie das richtige Modul für sharepoint. Verwenden Sie get-module zu finden, die verfügbaren Module.
Programmgesteuert, siehe mein Beitrag zum Thema:
http://www.nivot.org/2010/05/03/PowerShell20DeveloperEssentials1InitializingARunspaceWithAModule.aspx
-Oisin
Ich habe dieses Problem vor kurzem. In meinem Fall war ich weder in der Lage, um zu sehen, die Hinzugefügt, die Lösung noch in der Lage, die Lösung. Also zuerst Entferne ich die Lösung mit unten PowerShell-Befehl:
Dann konnte ich meine neue code-Lösung.
Auch sicherstellen, dass Sie runing "Add-SPSolution" Befehl von Web-Anwendungen, die auf IIS ausgeführt wird, und NICHT mit standard-Visual Studio-server (wenn Sie drücken Sie die F5-Taste).