使用AzureAD PowerShell模块时持续出现Newtonsoft.Json.JsonReaderException错误
解决AzureAD PowerShell模块JSON读取错误问题
问题现象
运行AzureAD模块任意命令均触发JSON读取错误,示例命令:
New-AzureADServiceAppRoleAssignment -Id "any id" -ResourceId "any id" -ObjectId "any id" -PrincipalID "any id"
错误输出:
Error reading JToken from JsonReader. Path '', line 0, position 0
At line:1 char:1
New-AzureADServiceAppRoleAssignment -Id ....
CategoryInfo : NotSpecified: (:) [New-AzureADServiceAppRoleAssignment], JsonReaderException
FullyQualifiedErrorId: Newtonsoft.Json.JsonReaderException.Microsoft.Open.AzureAD16.Powershell.NewServicePrincipalAppRoleAssignment.
解决步骤
卸载并重装AzureAD模块
先清理现有模块:Uninstall-Module -Name AzureAD -Force Uninstall-Module -Name AzureADPreview -Force # 若安装了预览版一并卸载重新安装稳定版:
Install-Module -Name AzureAD -Force -AllowClobber修复Newtonsoft.Json依赖
若模块重装后问题依旧:- 打开AzureAD模块默认安装目录:
C:\Program Files\WindowsPowerShell\Modules\AzureAD - 进入对应版本的文件夹,检查
lib目录下是否存在Newtonsoft.Json.dll - 若文件缺失或损坏,下载模块依赖的10.x版本Newtonsoft.Json包,将
Newtonsoft.Json.dll复制到该lib目录
- 打开AzureAD模块默认安装目录:
清理PowerShell缓存并重启
Remove-Module AzureAD -Force $env:PSModuleCache = ""重启PowerShell后重新导入模块:
Import-Module AzureAD检查PowerShell版本兼容性
AzureAD模块对PowerShell 7支持有限,建议切换到PowerShell 5.1环境重试。
内容的提问来源于stack exchange,提问作者user20826116
相关产品推荐
相关产品推荐

