问题
I used the "System.Management.Automation.dll" for executing PowerShell Commands from C# code. But I could not execute the Service Fabric Commands.
When I execute the Service Fabric commands i get the following error
The specified module 'ServiceFabric' was not loaded because no valid module file was found in any module directory
The term 'Connect-ServiceFabricCluster' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
The Code I used is
var shell = PowerShell.Create();
shell.Commands.AddScript("Import-Module ServiceFabric");
shell.Commands.AddScript("Connect-ServiceFabricCluster");
var result = shell.Invoke();
These Commands worked successfully when I executed them directly from the PowerShell command window.
The above code works fine for other PowerShell Commands
I also tried loading the Module using the physical path "C:\Windows\System32\WindowsPowerShell\v1.0\Modules\ServiceFabric" That didn't work either.
回答1:
Why are you trying to call powershell commands from C#? All the powershell commands have parity with API - meaning, for each command, there is an API you can use directly. Just create a FabricClient and call whatever methods you need.
来源:https://stackoverflow.com/questions/34526299/how-to-execute-service-fabric-powershell-commands-from-c