Execute azure CLI & kubectl commands from c# RRS feed

  • Question

  • Hi Team,

    I have requirement to automate some process related aks using azure function.

    My requirement is to dynamically identify the services in each aks cluster, identify IPs, create REST API url ,curl the installed application using this APIs.

    I can able to do all these operations using Python, without any issues, but unfortunately, Python based azure function doesn't support vnet integration (All my services are private and not publicly available).

    So I thought of the make use of the C# based windows azure function, unfortunately, I am unable to find resources to execute azure CLI commands and kubectl commands from C# code. (commands I need to run - az login,az account set,az aks get-credentials and kubectl get services).

    If anyone have any knowledge about to make use of the above commands in C#, please try to help me.

    I had some google search to find a solution, and I couldn't find anything useful for my requirement.


    Mebin Thomas


    Tuesday, May 19, 2020 3:10 PM

All replies

  • The accessibility of vnet integration with an Azure function is based on the plan type not on the runtime, so if you would like to use your Python script, could you share a little more about what errors you are having? I didn't have any trouble adding my Python-based function app to a vnet on the Premium plan.

    Outside of that, running CLI commands from a C# functions is going to be one of the more difficult options you could use. I would either make use of the SDKs for C# or run a Powershell function app with the CLI.


    There's an example of using the Azure SDKs for C# with AKS in this sample: https://github.com/Azure-Samples/aks-dotnet-manage-kubernetes-cluster/blob/master/Program.cs

    Kubernetes also provides an official C# SDK: https://github.com/kubernetes-client/csharp

    I agree that there won't be as many examples out there with these two combined, but it should be possible to use the two SDKs to accomplish what you want.


    The Azure CLI is included as a optional dependency for Powershell functions by default. Anything else needs to be installed from the Powershell gallery, so you would need to include install-kubectl as a dependency and go from there.

    C# + CLI
    As I mentioned, I highly recommend going with one of the other options, but it is possible to run Powershell scripts on .Net Core assuming a Windows host. It is dependent on the .NET Process class, which isn't yet fully supported in Linux, so a Windows host will give you more flexibility.

    Duane Newman wrote a good tutorial on how to do this here: https://duanenewman.net/blog/post/running-powershell-scripts-from-csharp/

    var psCommmand = @"echo ""quoted value"" 
        echo ""Second Line""
        var psCommandBytes = System.Text.Encoding.Unicode.GetBytes(psCommmand);
        var psCommandBase64 = Convert.ToBase64String(psCommandBytes);
        var startInfo = new ProcessStartInfo()
            FileName = "powershell.exe",
            Arguments = $"-NoProfile -ExecutionPolicy unrestricted -EncodedCommand {psCommandBase64}",
            UseShellExecute = false

    Thursday, May 21, 2020 4:55 PM
  • Thanks samara for the details and apologies for the provided partial details.

    My difficulties here are,

    In one RG, we can able to run only one type of azure function and this mentioned RG already having another windows based azure function. so I won't be able to create a python based azure function and make use of vnet.

    Anyway I look in to the provided solution provided and let you know the outcome soon.

    Wednesday, May 27, 2020 6:29 AM