“Connect-AzureAD”这个术语不被识别为 cmdlet 的名称。

24

在Azure AD中从C#应用程序运行PowerShell脚本。

添加以下DLL引用

  • 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();

遇到错误:

“Connect-AzureAD”这个名称无法识别为 cmdlet、函数、脚本文件或可操作的程序。请检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。


4
尝试先执行 Install-Module AzureAD 命令? - Mike
你是在构建 x64 吗?如果仅运行 Import-Module AzureAD -Force,你在错误流中得到了什么?(例如:Console.WriteLine(string.Join("\n", pipeline.Error.ReadToEnd().Select(err => err.ToString()))); - Philippe Signoret
另外,你正在做的事情可能比直接调用Azure AD Graph API要复杂得多。你有任何必须使用PowerShell模块的理由吗? - Philippe Signoret
Philippe Signoret,是的,我想要:
  1. 在Set-Mailbox中为离职用户设置ForwardingAddress。
  2. 更改OU Move-ADObject -Identity "OU=ManagedGroups,DC=Fabrikam,DC=Com" -TargetPath "OU=Managed,DC=Fabrikam,DC=Com"
这在Azure AD Graph API中不可用。
- user1638526
嗨,Mike,我按照你的建议添加了代码,但是出现了以下错误: 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
NuGet提供程序必须位于“C:\Program Filesx86)\PackageManagement\ProviderAssemblies”或“C:\Users\Admin\AppData\Local\PackageManagement\ProviderAssemblies”中可用。您还可以通过运行“Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force”来安装NuGet提供程序。您是否希望PowerShellGet现在安装并导入NuGet提供程序? - user1638526
2个回答

36

我在使用PowerShell v7时遇到了问题,与PS v5不同,你必须在安装后使用Import-Module AzureAD导入模块。否则会出现与在PSGallery等模块源中安装但未导入相同的错误。


13

@user1638526,正如迈克所提到的,您应首先安装AzureAD模块。

您可以按以下步骤操作:

Install-PackageProvider -Name NuGet -RequiredVersion 2.8.5.201 -Force

Import-PackageProvider -Name NuGet -RequiredVersion 2.8.5.201

Install-Module AzureAD -Force
-Force参数抑制了用户输入提示,允许脚本在后台运行。
参考:如何在未连接的计算机上安装PowerShell的Nuget提供程序,以便可以从PS命令行安装nuget包? 关于如何使用C#调用PowerShell命令或PS1文件,您还可以参考此链接或另一个SO线程
希望这有所帮助!

3
Install-PackageProvider: 未找到符合提供程序 'NuGet' 指定搜索条件的匹配项。该软件包提供程序需要 'PackageManagement' 和 'Provider' 标记。请检查指定的软件包是否具有这些标记。 - UserControl
"Get-InstalledModule" 的结果包含 AzureADPreview。"Get-PackageProvider -ListAvailable" 的结果包含 Nuget 和 PowerShellGet。正如 OP 所描述的那样,调用任何 AzureADPreview 命令都会导致错误:"...未被识别为 cmdlet、函数、脚本文件或可操作程序的名称"。 - Mark

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接