无法在 C# 控制台应用程序中执行 Azure CLI 命令

问题描述 投票:0回答:1

尝试通过 C# 控制台应用程序运行 Azure CLI 命令时,遇到异常。虽然没有构建错误,但项目的执行会导致错误。 这是我遇到的错误的快照:

这是我尝试运行的 C# 代码。

using System;
using System.Diagnostics;
using System.IO;


public class AzureCliCommandRunner
{
    public static string RunAzureCliCommand(string command)
    {
        var processStartInfo = new ProcessStartInfo
        {
            FileName = "az",
            Arguments = command,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };

        using (var process = new Process { StartInfo = processStartInfo })
        {
            process.Start();

            // Read the output and error streams
            string output = process.StandardOutput.ReadToEnd();
            string error = process.StandardError.ReadToEnd();

            process.WaitForExit();

            if (process.ExitCode == 0)
            {
                return output;
            }
            else
            {
                // Handle the error
                throw new InvalidOperationException($"Command execution failed. Error: {error}");
            }
        }
    }
    public static void UpdateAppConfigFilter(string endpoint, string featureName)
    {
        // Run Azure CLI commands to update AppConfig filter
        string command1 = $"appconfig feature filter show --connection-string {endpoint} --feature {featureName} --filter-name Microsoft.Targeting";
        string jsondata = RunAzureCliCommand(command1);

        string command2 = $"echo '{jsondata}' | jq -r '.[].parameters.Audience'";
        string audienceSection = RunAzureCliCommand(command2).Trim();

        string new_user = "newuser";
        string command3 = $"echo '{audienceSection}' | jq --arg new_user '{new_user}' '.Users += [\"$new_user\"]'";
        string file_content = RunAzureCliCommand(command3).Trim();

        string command4 = $"appconfig feature filter update --connection-string {endpoint} --feature {featureName} --filter-name Microsoft.Targeting --filter-parameters Audience='{file_content}' --yes";
        RunAzureCliCommand(command4);
    }

    public static void Main()
    {
        // Replace <<EndPoint>> and <<FeatureName>> with your actual values
        string endpoint = <<EndPoint>>;
        string featureName = <<FeatureName>>";

        UpdateAppConfigFilter(endpoint, featureName);
    }
}
c# azure-cli azure-app-configuration azure-feature-manager
1个回答
0
投票

首先,我也遇到了类似的错误,这里的文件名应该更改为az.cmd在系统中所在的位置。

我对你的代码进行了如下修改:

using System.Diagnostics;


public class AzureCliCommandRunner
{
    public static string RunAzureCliCommand(string command)
    {
        var processStartInfo = new ProcessStartInfo
        {
            FileName = @"C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\az.cmd",
            Arguments = command,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            UseShellExecute = false,
            CreateNoWindow = true

        };
        using (var process = new Process { StartInfo = processStartInfo })
        {
            process.Start();
            string output = process.StandardOutput.ReadToEnd();
            string error = process.StandardError.ReadToEnd();

            process.WaitForExit();

            if (process.ExitCode == 0)
            {
                return output;
            }
            else
            {
                throw new InvalidOperationException($"Command execution failed. Error: {error}");
            }
        }
    }
    public static void UpdateAppConfigFilter(string name)
    {
        string rith_command = $"group show --name  {name}";
        string rithdata = RunAzureCliCommand(rith_command);
        Console.WriteLine(rithdata);     
    }

    public static void Main()
    {
        string name = "rbojja";  
        UpdateAppConfigFilter(name);
    }
}

Output:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.