I am unable to successfully execute following piece of code. Whenever I try to call Azure commands through C#, it threw an exception ""The term 'Connect-AzureADUser' is not recognized as the name of a cmdlet, function, script file, or operable program. Check 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 addition to it, I should be able to see the output in the results variable.
PSDataCollection<PSObject> myOutPut = new PSDataCollection<PSObject>();
InitialSessionState initialState = InitialSessionState.CreateDefault();
initialState.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Bypass;
initialState.AuthorizationManager = new System.Management.Automation.AuthorizationManager("O365");
initialState.LanguageMode = System.Management.Automation.PSLanguageMode.FullLanguage;
initialState.ImportPSModule(new string[] { "AzureADPreview" });
Runspace runspace = RunspaceFactory.CreateRunspace(initialState);
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript("Connect-AzureAD");
pipeline.Commands.AddScript("Get-AzureADUser");
var result = pipeline.Invoke();