Running powershell script from C# application in Azure AD.
Added below DLL reference
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript("Import-Module AzureAD -Force;");
pipeline.Commands.AddScript("$password = ConvertTo-SecureString " + "\"abc1234\"" + " -AsPlainText -Force");
pipeline.Commands.AddScript("$Cred = New-Object System.Management.Automation.PSCredential (" + "\"[email protected]\"" + ", $password)");
pipeline.Commands.AddScript("Connect-AzureAD -Credential $Cred");
pipeline.Commands.AddScript("Get-AzureADApplication -Filter " + "\"DisplayName eq " + "\'PortalTestApp\'" + "\"");
var result = pipeline.Invoke();
Getting Error:
The term 'Connect-AzureAD' 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.
Type “PowerShell” from the start menu >> Right-click on Windows PowerShell and choose “Run as administrator” Type “Install-Module AzureAD” and hit Enter. You'll be asked to confirm the installation from the PSGallery. Type “A” to select “Yes to All” and hit the Enter key.
If that module is missing, corrupt, or got moved, it throws up the error, “the term is not recognized as the name of a cmdlet.” You can use “get-module” in PowerShell to see if the module is present and correct. It will show you what modules are loaded, and you can add or repair them depending on your needs.
Follow these steps to install the Microsoft Azure Active Directory Module for Windows PowerShell: Open an elevated Windows PowerShell command prompt (run Windows PowerShell as an administrator). Run the Install-Module MSOnline command. If you're prompted to install the NuGet provider, type Y and press Enter.
I had an issue with PowerShell v7, unlike PS v5, you have to import the module after installation with Import-Module AzureAD
. The error is identical to if you haven't imported it after installing it from a module source like PSGallery.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With