Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在c#代码中执行Service fabric cmdlet_C#_Powershell_System.management - Fatal编程技术网

在c#代码中执行Service fabric cmdlet

在c#代码中执行Service fabric cmdlet,c#,powershell,system.management,C#,Powershell,System.management,我按照http://jeffmurr.com/blog/?p=142用于从C#调用powershell脚本。但我得到的错误就像 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 inclu

我按照
http://jeffmurr.com/blog/?p=142
用于从C#调用powershell脚本。但我得到的错误就像

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.
如何使它成功

下面是我试过的代码。我将命令文本框的值传递为
connectservicefactriccluster-ConnectionEndpoint“localhost:19000”
"

public void部署微服务()
{
字符串结果=string.Empty;
ResultBox.Text=string.Empty;
//初始化PowerShell引擎
var shell=PowerShell.Create();
//将脚本添加到PowerShell对象
//shell.Commands.AddScript(Server.MapPath(“~”+“Powershell\\microservice.ps1”);
shell.Commands.AddScript(Commands.Text);
//执行脚本
var results=shell.Invoke();
如果(shell.Streams.Error.Count>0)
{
var builder=新的StringBuilder();
foreach(shell.Streams.error中的var错误)
{
builder.Append(error.ToString()+“\r\n”);
}
ResultBox.Text=Server.HtmlEncode(builder.ToString());
}
//显示结果,将BaseObject转换为字符串
//注意:对于类似控制台的输出,使用| out字符串
如果(results.Count>0)
{
//我们使用字符串生成器来创建结果文本
var builder=新的StringBuilder();
foreach(结果中的var psObject)
{
//将基础对象转换为字符串并将其附加到字符串生成器。
//为换行符添加\r\n
Append(psObject.BaseObject.ToString()+“\r\n”);
}
//用HTML编码字符串(防止出现“危险”字符的安全问题,如<>
ResultBox.Text=Server.HtmlEncode(builder.ToString());
}
}

我的最终目标是创建一个可以将应用程序部署到群集的站点。否则,我必须登录到安装SF的系统并手动执行power shell命令。

如果要使用它,您需要导入
ServiceFabric
模块:

InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(new[]{"ServiceFabric"});

var shell = PowerShell.Create(iss);

好的,我找到了解决方案。问题是因为我的应用程序在32位模式下运行,未加载service fabric cmdlet。我将我的应用程序转换为64位,现在cmdlet如下

Connect-ServiceFabricCluster -ConnectionEndpoint "localhost:19000"  
被识别

Connect-ServiceFabricCluster -ConnectionEndpoint "localhost:19000"