How to Execute Service Fabric Powershell commands from c#?

我们两清 提交于 2019-12-12 05:26:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!