1

As the question is quite intuitive, I'm trying to use AzureRM in my asp .net core web application. I've created InitialStateSession and Runspace as follows.

Runsapce Version: 7.0.3

Installed the AzureRM module from powershellGallary.

InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Bypass;
iss.ImportPSModule("AzureRM");
Runspace rso = RunspaceFactory.CreateRunspace(iss);
rso.Open();
PowerShell ps = PowerShell.Create();
ps.Runspace = rso;
ps.AddScript("Connect-AzureRmAccount");
var res = ps.Invoke();
var errors = ps.Streams.Error.ReadAll();
rso.Close();

when I run the code I get the following error

{The term 'Connect-AzureRmAccount' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.}

While using the same commands directly on PowerShell works like charm. but it doesn't with c# code.

I even tried Importing the module like below.

iss.ImportPSModule("path to the module.psm1 or .psd1"); and

ps.AddScript("Import-Module azureRM");

ps.AddScript("Import-Module azureRM.Profile");

I've checked for the paths it is looking for the module and it included the modules path using this.

var path = rso.SessionStateProxy.GetVariable("env:PSModulePath");

I've installed azureRM.Netcore also as there was some issue with versions.

Each tries to fix this is highly appreciated. thanks in advance.

Liam
  • 27,717
  • 28
  • 128
  • 190
Venkatesh Dharavath
  • 500
  • 1
  • 5
  • 18
  • This problem is really tricky and difficult to deal with. A lot of solutions have been tried but ultimately failed. I can only deal with this problem in a different way, and hope to inspire you. – Jason Pan Jul 22 '20 at 07:16

1 Answers1

1

After a long period of exploration and research, I found that the same error message will appear when both Connect-AzAccount and Connect-AzureRmAccount are executed, but the local PowerShell execution is normal, which is a strange phenomenon.

Checked a lot of information and closed issues on github. The solution is nothing more than installing AzureRM.NetCore or other vs plug-ins, but they have no effect. And the official does not have a good explanation.

In the end, I changed my mind, hoping to help you realize this function. Finally, if it succeeds, the command is executed through cmd.

        System.Diagnostics.Process p = new System.Diagnostics.Process();
        p.StartInfo.FileName = "cmd.exe";
        p.StartInfo.UseShellExecute = false;    
        p.StartInfo.RedirectStandardInput = true;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.CreateNoWindow = true;
        p.Start();

        p.StandardInput.WriteLine("Powershell Connect-AzureRmAccount &exit");

        p.StandardInput.AutoFlush = true;
Jason Pan
  • 15,263
  • 1
  • 14
  • 29
  • thanks for your answer, I did not try to execute your solution though. I tried the same code as posted in the question with Az and it works very well. And I started migrating to Az already as anyhow azureRM will be deprecated after 2 years. – Venkatesh Dharavath Jul 23 '20 at 23:24
  • @Venkateshdharavath Okay, I'm glad you can solve the problem with `Connnect-AzAccount`. When using `powershell` successfully in the local, but fails by code, my solutions can be tried. – Jason Pan Jul 24 '20 at 01:06
  • Thanks for your suggestion @Jason Pan – Venkatesh Dharavath Jul 24 '20 at 03:05
  • @Venkateshdharavath Although you passed `Az` to solve your problem, but because there is a problem on `AzureRm`, which leads to the problem, my solution is to help you solve the problem of `AzureRm`, of course. Az` also works. – Jason Pan Jul 24 '20 at 21:17