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

%3CLINGO-SUB%20id%3D%22lingo-sub-2547121%22%20slang%3D%22en-US%22%3EUse%20'Connect-AzAccount'%20command%20of%20PowerShell%20in%20C%23%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2547121%22%20slang%3D%22en-US%22%3E%3CP%3EI%20want%20to%20connect%20to%20Azure%20the%20same%20way%20I%20log%20in%20using%20Powershell%20commands.%20Since%20I%20have%20not%20found%20any%20API%20to%20do%20that%20with%20.NET%2C%20I%20thought%20that%20a%20good%20idea%20would%20be%20to%20execute%20PS%20scripts%20with.NET.%3C%2FP%3E%3CP%3EI%20have%20already%20installed%20the%20Powershell%20for%20azure%20on%20my%20laptop%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%26nbsp%3BInstall-Module%20-Name%20Az%20-AllowClobber%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%3CBR%20%2F%3EI%20also%20tried%20to%20use%20the%20'Connect-AzAccount'%20command%20just%20with%20the%20Windows%20Powershell%20of%20my%20PC%20and%20it%20works%20fine.%3C%2FP%3E%3CP%3ESo%2C%20I%20tried%20the%20following%20in%20Visual%20Studio%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Enamespace%20AzureTestVM%0A%7B%0A%20%20%20%20class%20Program%0A%20%20%20%20%7B%0A%0A%20%20%20%20%20%20%20%20static%20void%20Main(string%5B%5D%20args)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20PowerShell%20ps%20%3D%20PowerShell.Create().AddCommand(%22Connect-AzAccount%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20ps.Invoke()%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%3CBR%20%2F%3EThe%20problem%20is%20that%20I%20get%20the%20following%20error%20%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EUnhandled%20Exception%3A%20System.Management.Automation.CmdletInvocationException%3A%20Method%20'get_SerializationSettings'%20on%20type'%20Microsoft.Azure.Management.Internal.Resources.ResourceManagementClient%20'of%20assembly'%20Microsoft.Azure.PowerShell.Clients.ResourceManager%2C%20Version%20%3D%201.0.%200.0%2C%20Culture%20%3D%20neutral%2C%3CBR%20%2F%3EPublicKeyToken%20%3D%2031bf3856ad364e35%20'does%20not%20have%20an%20implementation.%20---%26gt%3B%20System.TypeLoadException%3A%20Method%20'get_SerializationSettings'%20on%20type'%3CBR%20%2F%3EMicrosoft.Azure.Management.Internal.Resources.ResourceManagementClient%20'of%20assembly'%20Microsoft.Azure.PowerShell.Clients.ResourceManager%2C%20Version%20%3D%201.0.0.0%2C%20Culture%20%3D%20neutral%2C%3CBR%20%2F%3EPublicKeyToken%20%3D%2031bf3856ad364e35%20'does%20not%20have%20an%20implementation.%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

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 thought that a good idea would be to execute PS scripts with.NET.

I have already installed the Powershell for azure on 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:

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.

0 Replies