Search code examples
c#asp.netazurepowershellrunspace

Why am I not able to login to Azure using power shell commands that are being executed from ASP.Net code?


I have created an ASP.Net Web application. From this application, I am trying to execute power shell commands to log in to Azure. The command being used is 'az login'. When I run the application, I get proper response that is expected from a login command, the response tells me that the login is successful. The problem though, is that when I host the same application in IIS, I don't get the expected result, instead I get an empty response which indicates that there is some problem. Below is the code that I am using:

        Runspace runspace = RunspaceFactory.CreateRunspace();
        runspace.Open();
        Pipeline pipeline = runspace.CreatePipeline();
        pipeline.Commands.AddScript("az login -u [email protected] -p ********");
        pipeline.Commands.Add("Out-String");
        var output = pipeline.Invoke();
        string result = output[0].ToString();

Please suggest what might be wrong.


Solution

  • It has to do with the application pool identity.

    Change application pool identity to your account instead of default and it should work.