c# - 术语 'Connect-AzureAD' 未被识别为 cmdlet 的名称

标签 c# powershell azure-active-directory

从 Azure AD 中的 C# 应用程序运行 powershell 脚本。

添加到 DLL 引用下方

  • 系统.管理.自动化
  • 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();

获取错误:

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.

最佳答案

我在使用 PowerShell v7 时遇到问题,与 PS v5 不同,您必须在安装后使用 Import-Module AzureAD 导入模块。该错误与从 PSGallery 等模块源安装后未导入它相同。

关于c# - 术语 'Connect-AzureAD' 未被识别为 cmdlet 的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56543597/

相关文章:

azure - 无法更新 Azure AD 应用程序的 list 文件中的identifierUris

c# - Azure 图形 API 不适用于上一页请求 (&previous-page=true)

c# - 从 Outlook 电子邮件中获取正文 [Drag'and'Drop]

powershell - PowerShell别名分配工厂

c# - 如何在不使用任何 OpenSource Dll 的情况下使用 C# 4.0 从文件夹中解压所有 .Zip 文件?

string - 将字符串拆分为单独的变量

powershell - 使用 Powershell 获取上次登录时间、计算机和用户名

azure - 如何配置 Azure AD 应用程序注册重定向 URL 以适用于本地主机和 Azure 部署?

c# - 从 Lucene 查询中获取不同值的更快方法

c# - 我怎么知道一个方法是否不是从基类 C# 派生的