1
votes

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
Your process have same bitness as OS?user4003407
Changed the Application to 64 bit and it worked.Binu Vijayan

1 Answers

1
votes

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.