c# - 如何从 C# 执行 Service Fabric Powershell 命令?

标签 c# powershell azure azure-service-fabric

我使用“System.Management.Automation.dll”从 C# 代码执行 PowerShell 命令。但我无法执行 Service Fabric 命令。

当我执行 Service Fabric 命令时,出现以下错误

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.

我使用的代码是

 var shell = PowerShell.Create();
 shell.Commands.AddScript("Import-Module ServiceFabric");
 shell.Commands.AddScript("Connect-ServiceFabricCluster");
 var result = shell.Invoke();

当我直接从 PowerShell 命令窗口执行这些命令时,这些命令成功运行。

上面的代码适用于其他 PowerShell 命令

我还尝试使用物理路径加载模块 “C:\Windows\System32\WindowsPowerShell\v1.0\Modules\ServiceFabric” 那也没用。

最佳答案

为什么要尝试从 C# 调用 powershell 命令?所有 powershell 命令都与 API 具有同等性 - 这意味着,对于每个命令,都有一个可以直接使用的 API。只需创建一个 FabricClient 并调用您需要的任何方法即可。

关于c# - 如何从 C# 执行 Service Fabric Powershell 命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34526299/

相关文章:

c# - 为什么我的DLL可以编译为比我选择的版本低的.NET版本?

Powershell - 删除注册表项中的所有属性

linux - 如何使用 Azure 为资源管理器 VM 提供的 DNS?

c# - XAML 设计器找不到资源

c# - ASP.NET - Comet 将消息从服​​务器推送到所有客户端

c# - 在多层项目中管理配置的方法

arrays - 如果仅前6个字符不同,如何计算行数?

function - 使用 powershell 更改运行时版本 azure 函数

python - 使用 python 连接 sql server 时,此版本的 SQL Server 不支持 Azure Windows 登录

Azure CosmosDB 定价 - 有多少 RT