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