0

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

4

2 回答 2

1

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

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

var shell = PowerShell.Create(iss);
于 2017-11-05T11:10:00.903 回答
0

好的,我找到了灵魂。问题是因为我的应用程序在 32 位模式下运行,因此未加载服务结构 cmdlet。我将我的应用程序转换为 64 位,现在 cmdlet 像

Connect-ServiceFabricCluster -ConnectionEndpoint "localhost:19000"  

被识别。

于 2017-11-12T04:20:46.633 回答