Calling Azure Powershell commands from .net Core 3.1

Prashu 56 Reputation points
2020-10-08T20:55:38.88+00:00

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 https://www.nuget.org/packages/Microsoft.PowerShell.5.ReferenceAssemblies/ and it worked well.
Unfortunately this package is not supported in .net core 3.1

InitialSessionState initialState = InitialSessionState.CreateDefault();
initialState.ImportPSModule("AzureAD");
Runspace runspace = RunspaceFactory.CreateRunspace(initialState);
runspace.Open();
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 : https://learn.microsoft.com/en-us/answers/questions/25056/calling-azure-powershell-commands-from-c.html

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,166 questions
{count} votes

Accepted answer
  1. 2020-10-09T20:12:50.093+00:00

    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