Not able to run powershell commands from ASP.Net code

I have an ASP.Net application that tries to execute the ‘az login’ command on Azure using power shell commands. Below is the code that I am using:

[pre]

PowerShell powershell = PowerShell.Create();
using (Runspace runspace = RunspaceFactory.CreateRunspace())
{
runspace.Open();
powershell.Runspace = runspace;
PSCommand PScmd = new PSCommand();
string cmdStr = “az login -u ‘vtripathi@hsdyn.com’ -p ‘*******’”;
powershell.AddScript(cmdStr);
var results = powershell.Invoke();
}
}

[/pre]

 

When this code is executed locally, it runs successfully, but when I deploy the same code on Azure app service and browsethe service, I get an error message saying ‘az is not recognized as the name of a cmdlet’. I even went to the ‘Kudu’ powershell where the code is deployed and tried to run the same command there but still got the same error. Please suggest what needs to be done in this case.

See these articles

Calling a PowerShell Script From Your .NET Code

Using PowerShell 2.0 from ASP.NET Part 1

Making a powershell script run from aspx

Execute PowerShell from a ASP.NET Web Application