Calling Azure Powershell commands from .net Core 3.1

Prashu 56 Reputation points

Whenever I try to call Azure commands through .net core 3.1, it threw an exception I still get the error '{"The term 'Connect-AzureAD' is not recognized as the name of a cmdlet, function, script file, or operable program.\r\nCheck the spelling of the name, or if a path was included, verify that the path is correct and try again.'. Is there a way to execute Azure powershell commands through C# ?

In the past I had tried using .net Framework with the package and it worked well.
Unfortunately this package is not supported in .net core 3.1

InitialSessionState initialState = InitialSessionState.CreateDefault();
Runspace runspace = RunspaceFactory.CreateRunspace(initialState);
Pipeline pipeline;
Collection<PSObject> result = new Collection<PSObject>();
pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript("Connect-AzureAD -TenantId " + tenantId + " -ApplicationId " +
applicationId + " -CertificateThumbprint " + certThumb);
var result = pipeline.Invoke(); --> This line fails and same code works in .net Framework

Reference :

A set of technologies in the .NET Framework for building web applications and XML web services.
3,998 questions
{count} votes

Accepted answer
  1. Alfredo Revilla (MSFT) 26,821 Reputation points

    Maybe you need to import the module first. However AzureAD works with PS 5 not 7 (Core) which could possibily throw errors related to types not loaded.

    Please let us know if this answer was helpful to you. If so, please remember to mark it as the answer so that others in the community with similar questions can more easily find a solution.

0 additional answers

Sort by: Most helpful