【问题标题】:Cannot import AzureAD module in C#无法在 C# 中导入 AzureAD 模块
【发布时间】:2017-11-09 06:03:11
【问题描述】:

以前有人遇到过这个问题吗?我计划使用 C# 来调用 AzureAD cmdlet。但是我尝试了很多方法来导入模块,比如:

InitialSessionState initialState = InitialSessionState.CreateDefault();
initialState.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Bypass;
initialState.AuthorizationManager = new System.Management.Automation.AuthorizationManager("O365");
initialState.LanguageMode = System.Management.Automation.PSLanguageMode.FullLanguage;
initialState.ImportPSModule(new string[] {"AzureAD" });
Runspace runspace = RunspaceFactory.CreateRunspace(initialState);

或者

pipeline.Commands.AddScript("Import-Module -Name AzureAD -Force; Get-Module");
var modules = pipeline.Invoke();

或者

pipeline.Commands.AddScript("Import-Module");
pipeline.Commands[0].Parameters.Add("Name", "AzureAD");
var modules = pipeline.Invoke()

没有人可以导入该模块。即使我使用完整路径“C:\Program Files\WindowsPowerShell\Modules\AzureAD\2.0.0.131\AzureAD.psd1”。我已使用 cmdlet“Install-Module AzureAD -Force”在我的服务器中安装 AzureAD 模块。当我在 C# 中调用 import-module 时没有错误,但是当我尝试使用 AzureAD cmdlet(如 'Connect-AzureAD')时,我会收到错误消息:

在模块“AzureAD”中找到了“Connect-AzureAD”命令,但无法加载该模块。

两个System.Management.Automation.dll我都试过了,问题是一样的。我试过powershell 4.0、5.0。

请问有人有什么想法吗?非常感谢。

顺便说一句,第一版 Azure AD 模块 MSOnline 运行良好。

我在 import-module AzureAD cmdlet 中启用了 -Verbose 日志,然后我检查了 PowerShell.Streams.Verbose 中的 VERBOSE 输出,发现只有一条记录:

从路径“C:\Program Files (x86)\WindowsPowerShell\Modules\AzureAD\2.0.0.131\AzureAD.psd1”加载模块。

但它应该有几十个详细的输出,就像它在 powershell 命令提示符中所做的那样:

谢谢

-贾斯汀

【问题讨论】:

    标签: c# powershell azure azure-active-directory


    【解决方案1】:

    我也可以在我身边复制它。我通过取消勾选Prefer 32-bit平台目标解决了这个问题,更多细节请参考截图。

    当我安装 AzureADPreview 模块时,我用于测试的 C# 代码:

    Runspace runspace = RunspaceFactory.CreateRunspace();
    runspace.Open();
    Pipeline pipeline = runspace.CreatePipeline();
    pipeline.Commands.AddScript("Import-Module AzureADPreview -Force;");
    pipeline.Commands.AddScript("Connect-AzureAD");   
    var result = pipeline.Invoke();
    

    【讨论】:

    • 很好的答案。实际上我只是自己解决它,但是是的,很好。我通过使用 Powershell.Streams.Error 日志找到了这一点。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2021-07-27
    • 2021-09-25
    • 1970-01-01
    • 2015-05-08
    • 2021-12-29
    • 2020-06-06
    • 2019-12-15
    • 2014-08-09
    相关资源
    最近更新 更多