Reputation: 2714
I have a running and tested .ps1 script to add a WSP solution to specific SharePoint web application.
Now i am trying to run this script and send parameters to it using C#.
First lines of 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
} ......
I used the below method to send parameter to script and run it:
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;
}
}
It throws:
System.Management.Automation.CommandNotFoundException: The term 'add-spsolution' is not recognized as the name of a cmdlet, function, script file, or operable program.
Any suggestions?
Upvotes: 1
Views: 670
Reputation: 1
Please check if the application configuration is set to any cpu or x64 and the 64 bit powershell is getting invoked else it might throw the same error.
Upvotes: 0
Reputation: 3451
I'm not familiar with the Sharepoint PS scripts, but I'd guess that Add-SPSolution comes from a PS Sharepoint module. I'd further guess you loaded that module in the PS session where you tested you script - either manually or with your profile.
However programmatically executing PS doesn't run your profile. So you need to add invocations of the PS commands that you script depends on to your program. It may be as simple as just adding ipmo SharepointModuleOrWhateverItIsCalled
via runSpaceInvoker.Invoke
Upvotes: 1