I have the following code to run powershell script (standard Deploy-FabricApplication.ps1 shipped with Service Fabric project template) from desktop app:
using (var shell = PowerShell.Create())
{
Environment.CurrentDirectory = tmpPath;
var deploymentScriptPath = Path.Combine(tmpPath, "Scripts", "Deploy-FabricApplication.ps1");
var command = new Command(deploymentScriptPath);
shell.AddScript("Import-Module ServiceFabric");
command.Parameters.Add("PublishProfileFile", Path.Combine(tmpPath, "PublishProfiles\\Local.5Node.xml"));
command.Parameters.Add("ApplicationPackagePath", Path.Combine(tmpPath, "pkg\\Release"));
shell.Commands.AddCommand(command);
shell.Invoke();
}
However shell contains error after script invocation:
- The specified module 'ServiceFabric' was not loaded because no valid module file was found in any module directory.
- Unable to find type
[System.Fabric.FabricObjectClosedException]
There are another errors, but they caused because ServiceFabric module wasn't loaded. But this script runs successfully from powershell window, even without admin privileges with RemoteSigned execution policy. So is there any way to run this script from C# with all modules loaded like in Poweshell ISE?