在 C# 代码中执行服务结构 cmdlet

Execute Service fabric cmdlets in c# code

我按照 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命令。

需要导入ServiceFabric模块才能使用:

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

var shell = PowerShell.Create(iss);

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

这样的 cmdlet
Connect-ServiceFabricCluster -ConnectionEndpoint "localhost:19000"  

已确定。