How to run Below azure cli command/Script using C# code in Web application
Below code is in featuremanager.cli file
endpoint=<<EndPoint>>
featurename=<<FeatureName>>
jsondata=$(az appconfig feature filter show --connection-string $endpoint --feature $featurename --filter-name Microsoft.Targeting)
audiencesection=$(echo "$jsondata" | jq -r '.[].parameters.Audience')
echo $audiencesection
new_user='newuser'
file_content=$(echo "$audiencesection" | jq --arg new_user "$new_user" '.Users += [$new_user]')
echo "$file_content"
az appconfig feature filter update --connection-string $endpoint --feature $featurename --filter-name Microsoft.Targeting --filter-parameters Audience="$file_content" --yes
Below code is in HomeController.cs file
public async Task<IActionResult> Index()
{
ProcessStartInfo psi=new ProcessStartInfo("cmd");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.CreateNoWindow = true;
psi.RedirectStandardInput = true;
var proc=Process.Start(psi);
proc.StandardInput.WriteLine(@".\featuremanager.cli");
proc.StandardInput.WriteLine("exit");
string s=proc.StandardOutput.ReadToEnd();
return View();
}
When I try to run this code cli file is not executing and the code got directly exited.
In a web application scenario, the execution of command-line scripts using
Process.Start
may not work as expected due to limitations or restrictions in the web hosting environment.HomeController.cs:
appsettings.json
).Microsoft.Extensions.Configuration.AzureAppConfiguration
.Below is the sample script taken to test.
I was already in log-in with my AZ account in my Azure CLI.
Result: