10
votes

Running powershell script from C# application in Azure AD.

Added below DLL reference

  • System.Management.Automation
  • Microsoft.Online.Administration.Automation.PSModule.Resources
  • Microsoft.Online.Administration.Automation.PSModule

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.

2
Install-Module AzureAD Try that first?Mike
Are you building for x64? What do you get in the error stream if you only run Import-Module AzureAD -Force? (E.g. Console.WriteLine(string.Join("\n", pipeline.Error.ReadToEnd().Select(err => err.ToString())));)Philippe Signoret
Also, what you're doing is probably much more complicated than directly calling the Azure AD Graph API. Do you have any reason you must use the PowerShell module?Philippe Signoret
Philippe Signoret Yes, I want to 1. Set ForwardingAddress in Set-Mailbox for OffBoarding user. 2. Change OU Move-ADObject -Identity "OU=ManagedGroups,DC=Fabrikam,DC=Com" -TargetPath "OU=Managed,DC=Fabrikam,DC=Com" This is not available with Azure AD Graph API.user1638526
Hi Mike, I added as you suggested, i am getting following error, Exception calling "ShouldContinue" with "2" argument(s): A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories.user1638526

2 Answers

15
votes

I had an issue with PowerShell v7, unlike PS v5, you have to import the module with Import-Module AzureAD. It's maybe not related to the OP question, but the error is identical.

5
votes

@user1638526 As mike mentioned, you should install the AzureAD module first.

You can follow the below steps:

Install-PackageProvider -Name NuGet -RequiredVersion 2.8.5.201 -Force

Import-PackageProvider -Name NuGet -RequiredVersion 2.8.5.201

Install-Module AzureAD -Force

-Force suppresses the user input prompts and allows the script to run in the background.

Reference: How do I install the Nuget provider for PowerShell on a unconnected machine so I can install a nuget package from the PS command line?

About for how to call PowerShell command or PS1 file using C# you can also refer to link or another SO Thread.

Hope this helps!