AzureAD模块无法导入/加载二进制文件问题求助
解决Intune PowerShell示例脚本的ADAL程序集加载错误
我从GitHub克隆了microsoftgraph/powershell-intune-samples仓库,想用里面的API调用简化Intune设备管理,但自己对Azure和PowerShell经验不多,一直碰到下面的错误:
Line | 87 | [System.Reflection.Assembly]::LoadFrom($adal) | Out-Null | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Exception calling "LoadFrom" with "1" argument(s): "Could not load file or assembly 'C:\User\Documents\PowerShell\Modules\AzureAD\2.0.2.182\Microsoft.IdentityModel.Clients.ActiveDirectory.dll'. The system cannot find the file | specified." MethodInvocationException: C:\Users\Desktop\PS_Scripts\powershell-intune-samples-master\ManagedDevices\ManagedDevices_DeviceOwnership_Set.ps1:89:1 Line | 89 | [System.Reflection.Assembly]::LoadFrom($adalforms) | Out-Null | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Exception calling "LoadFrom" with "1" argument(s): "Could not load file or assembly 'C:\Users\Documents\PowerShell\Modules\AzureAD\2.0.2.182\Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll'. The system cannot find the | file specified." Cannot find type [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext]: verify that the assembly containing this type is loaded.
我已经用管理员权限的PowerShell安装并加载了AzureAD模块,验证过安装成功,但模块安装时没同步下载指定的Microsoft.IdentityModel.Clients.ActiveDirectory和Microsoft.IdentityModel.Clients.ActiveDirectory.Platform二进制文件,复制Visual Studio其他项目的同类文件后反而出现更多错误。
解决方法:
- 卸载现有AzureAD模块,换成AzureADPreview模块:
依次运行以下命令:
AzureADPreview模块会完整附带所需的ADAL程序集文件,不会出现缺失问题。Uninstall-Module -Name AzureAD -Force Install-Module -Name AzureADPreview -Force -AllowClobber - 修改脚本中的程序集路径变量:
脚本里的$adal和$adalforms可能硬编码了旧模块的路径,替换成动态读取AzureADPreview模块的文件路径:$adal = (Get-ChildItem -Path (Get-Module AzureADPreview -ListAvailable).ModuleBase -Filter "Microsoft.IdentityModel.Clients.ActiveDirectory.dll" -Recurse).FullName $adalforms = (Get-ChildItem -Path (Get-Module AzureADPreview -ListAvailable).ModuleBase -Filter "Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll" -Recurse).FullName - 不要手动复制第三方程序集:
不同版本的ADAL程序集存在兼容性差异,手动复制其他项目的文件会导致版本不匹配,引发更多异常,必须使用对应PowerShell模块自带的程序集文件。
内容的提问来源于stack exchange,提问作者James Clarke
相关产品推荐
相关产品推荐

