我遵循了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 命令。