24

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 (" + "\"abc@abc.com\"" + ", $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.

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
user1638526
  • 253
  • 1
  • 3
  • 6
  • 4
    `Install-Module AzureAD` Try that first? – Mike Jun 11 '19 at 12:56
  • 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 Jun 11 '19 at 13:10
  • 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 Jun 11 '19 at 13:10
  • 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 Jun 11 '19 at 13:19
  • 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 Jun 11 '19 at 13:44
  • [-] The NuGet provider must be available in 'C:\Program Filesx86)\PackageManagement\ProviderAssemblies' or 'C:\Users\Admin\AppData\Local\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider by running 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install and import the NuGet provider now? – user1638526 Jun 11 '19 at 13:45

2 Answers2

36

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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Shadoweb
  • 5,812
  • 1
  • 42
  • 55
13

@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!

AmanGarg-MSFT
  • 1,123
  • 6
  • 10
  • 3
    "Install-PackageProvider: No match was found for the specified search criteria for the provider 'NuGet'. The package provider requires 'PackageManagement' and 'Provider' tags. Please check if the specified package has the tags." – UserControl May 23 '20 at 07:35
  • "Get-InstalledModule" results contain AzureADPreview. "Get-PackageProvider -ListAvailable" results contains Nuget and PowerShellGet. As OP describes, calling any AzureADPreview command results in the error "...not recognized as the name of a cmdlet, function, script file, or operable program" – Mark Aug 31 '20 at 17:46