无法在C#

时间:2017-11-09 06:03:11

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

以前有人遇到过这个问题吗?我计划使用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"安装模块AzureAD -Force"在我的服务器中安装AzureAD模块。在C#中调用import-module时没有错误,但是当我尝试使用AzureAD cmdlet时,例如Connect-AzureAD'我将收到错误消息:

' Connect-AzureAD'命令在模块' AzureAD'中找到,但无法加载模块。

我已经尝试了2个System.Management.Automation.dll,问题是一样的。我尝试过PowerShell 4.0,5.0。 enter image description here

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

顺便说一句,第一版Azure AD模块MSOnline工作正常。

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

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

但它应该有与powershell命令提示符一样的数十个详细输出:

enter image description here

由于

-Justin

1 个答案:

答案 0 :(得分:4)

我也可以在我这边复制它。我通过取消选中首选32位平台目标来解决此问题,详情请参阅截图。

enter image description here

当我安装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();

enter image description here