如何从C#执行Service Fabric Powershell命令? [英] How to Execute Service Fabric Powershell commands from c#?
问题描述
我使用"System.Management.Automation.dll"从C#代码执行PowerShell命令.但是我无法执行服务结构命令.
I used the "System.Management.Automation.dll" for executing PowerShell Commands from C# code. But I could not execute the Service Fabric Commands.
当我执行Service Fabric命令时,出现以下错误
When I execute the Service Fabric commands i get the following error
未加载指定的模块'ServiceFabric',因为无效 在任何模块目录中都找到了模块文件
The specified module 'ServiceFabric' was not loaded because no valid module file was found in any module directory
术语"Connect-ServiceFabricCluster"不被识别为名称 cmdlet,函数,脚本文件或可运行程序的名称.检查 名称的拼写,或者是否包含路径,请验证该路径 是正确的,然后重试.
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命令窗口中执行这些命令时,这些命令成功运行.
These Commands worked successfully when I executed them directly from the PowerShell command window.
上面的代码可用于其他PowerShell命令
The above code works fine for other PowerShell Commands
我还尝试使用物理路径加载模块 "C:\ Windows \ System32 \ WindowsPowerShell \ v1.0 \ Modules \ ServiceFabric" 那也不起作用.
I also tried loading the Module using the physical path "C:\Windows\System32\WindowsPowerShell\v1.0\Modules\ServiceFabric" That didn't work either.
推荐答案
为什么要尝试从C#调用Powershell命令?所有powershell命令都具有与API相同的功能-意思是,对于每个命令,都有一个可以直接使用的API.只需创建一个FabricClient并调用所需的任何方法即可.
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.
这篇关于如何从C#执行Service Fabric Powershell命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!