J'ai un script .ps1 en cours d'exécution et testé pour ajouter une solution WSP à une application Web SharePoint spécifique.Exécutez le script Powershell à partir de C# pour ajouter une solution wsp à l'application sharepoint
Maintenant, j'essaie d'exécuter ce script et de lui envoyer des paramètres en utilisant C#.
Premières lignes de script:
param([String]$wsppath , [String]$webappurl)
Add-PsSnapin Microsoft.SharePoint.PowerShell
#Do not modify anything in the script from here onwards
function Get-ScriptDirectory
{
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
} ......
J'utilisé la méthode ci-dessous pour envoyer paramètre de script et l'exécuter:
public static bool ExecutePowerShellScript(List<String> args)
{
try
{
// create Powershell runspace
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runspace);
runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
String powershellScriptPath = System.AppDomain.CurrentDomain.BaseDirectory + "AutomateDeploymentScript.ps1";
// create a pipeline and feed it the script text
Pipeline pipeline = runspace.CreatePipeline();
Command command = new Command(powershellScriptPath);
foreach (var arg in args)
{
command.Parameters.Add(null, arg);
}
pipeline.Commands.Add(command);
pipeline.Invoke();
runspace.Close();
return true;
}
catch (Exception ex)
{
LoggingManager.LogException(ex);
return false;
}
}
Il lance:
System.Management. Automation.CommandNotFoundException: Le terme 'add-spsolution' n'est pas reconnu comme le nom d'une cmdlet, d'une fonction, d'un fichier de script ou d'un programme utilisable.
Des suggestions?
Est-ce que votre script AutomateDeploymentScript.ps1 contient Add-PSSnapin Microsoft.SharePoint.Powershell en haut? – DanL
@DanL J'ai mis à jour la question avec les premières lignes de script, la première ligne est la ligne de paramètres, j'ai testé le script individuellement et cela fonctionne correctement – Marzouk