我跟着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.
如何使其成功。
以下是我试过的代码。我将命令文本框的值传递为Connect-ServiceFabricCluster -ConnectionEndpoint "localhost:19000"
"
public void DeployMicroservices()
{
string result = string.Empty;
ResultBox.Text = string.Empty;
// Initialize PowerShell engine
var shell = PowerShell.Create();
// Add the script to the PowerShell object
//shell.Commands.AddScript(Server.MapPath("~")+"Powershell\\microservice.ps1");
shell.Commands.AddScript(commands.Text);
// Execute the script
var results = shell.Invoke();
if (shell.Streams.Error.Count > 0)
{
var builder = new StringBuilder();
foreach (var error in shell.Streams.Error )
{
builder.Append(error.ToString() + "\r\n");
}
ResultBox.Text = Server.HtmlEncode(builder.ToString());
}
// display results, with BaseObject converted to string
// Note : use |out-string for console-like output
if (results.Count > 0)
{
// We use a string builder ton create our result text
var builder = new StringBuilder();
foreach (var psObject in results)
{
// Convert the Base Object to a string and append it to the string builder.
// Add \r\n for line breaks
builder.Append(psObject.BaseObject.ToString() + "\r\n");
}
// Encode the string in HTML (prevent security issue with 'dangerous' caracters like < >
ResultBox.Text = Server.HtmlEncode(builder.ToString());
}
}
我的最终目标是创建一个可以将应用程序部署到集群的站点。否则我必须登录安装了SF的系统并手动执行power shell命令。
答案 0 :(得分:0)
如果要使用它,则需要导入ServiceFabric
模块:
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(new[]{"ServiceFabric"});
var shell = PowerShell.Create(iss);
答案 1 :(得分:0)
好的,我找到了灵魂。问题是因为我的应用程序以32位模式运行,因此未加载服务结构cmdlet。我把我的应用程序改为64位,现在像
这样的cmdletConnect-ServiceFabricCluster -ConnectionEndpoint "localhost:19000"
确定了。