question

Juanjo-9227 avatar image
0 Votes"
Juanjo-9227 asked TimonYang-MSFT commented

Use 'Connect-AzAccount' command of PowerShell in C#

I want to connect to Azure the same way I log in using Powershell commands. Since I have not found any API to do that with .NET, I though that a good dea would be to execute PS scripts with .NET.

I have already installed the Powershell for azure in my laptop

 Install-Module -Name Az -AllowClobber

I also tried to use the 'Connect-AzAccount' command just with the Windows Powershell of my PC and it works fine.

So, I tried the following in Visual Studio:

 using System.Management.Automation;

 namespace AzureTestVM
 { 
     class Program
     {

         static void Main(string[] args)
         {
             PowerShell ps = PowerShell.Create().AddCommand("Connect-AzAccount");
             ps.Invoke();
         }
     }
 }


The problem is that I get the following error:

Unhandled Exception: System.Management.Automation.CmdletInvocationException: Method 'get_SerializationSettings' on type' Microsoft.Azure.Management.Internal.Resources.ResourceManagementClient 'of assembly' Microsoft.Azure.PowerShell.Clients.ResourceManager, Version = 1.0. 0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35 'does not have an implementation. ---> System.TypeLoadException: Method 'get_SerializationSettings' on type' Microsoft.Azure.Management.Internal.Resources.ResourceManagementClient 'of assembly' Microsoft.Azure.PowerShell.Clients.ResourceManager, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35 'does not have an implementation.

dotnet-csharpwindows-server-powershell
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Juanjo-9227
According to this issue on GitHub, this issue is known but there is no solution yet.
Exception when executing Connect-AzAccount from C#
But I saw this post while searching, you can try to see if it works.
Method ‘get_SerializationSettings’ error when Login-AzureRMAccount

0 Votes 0 ·

0 Answers