MacOS下执行PowerShell脚本获取SharePoint站点模板时JSON转Int32报错
问题详情
- 运行环境:MacOS + Visual Studio Code + PowerShell
- 异常现象:脚本可正常连接SharePoint站点并获取网站标题,但执行
Get-PnPSiteTemplate -OutputInstance时触发错误:The JSON value could not be converted to System.Int32. Path: $.position.sectionIndex | LineNumber: 0 | BytePositionInLine: 107
- 已尝试操作:安装指定版本PowerShell,以及.NET SDK 3.1.425、6.0.403、7.0.100,问题未解决
- 执行脚本:
Connect-PnPOnline -Url $SiteURL -Credentials $Cred $web = Get-PnpWeb Write-Host "Test Connect: ", $web.Title $template = Get-PnPSiteTemplate -OutputInstance Write-Output $template Disconnect-PnPOnline
解决思路与操作步骤
1. 排查站点页面的Web部件配置
报错指向sectionIndex字段,说明站点内某个页面的Web部件位置信息中,该字段的值不是合法整数(比如为字符串、null或非数字格式)。需要:
- 检查站点内所有自定义页面、新建页面的Web部件布局
- 找到存在异常sectionIndex配置的Web部件,调整其位置或重新添加该Web部件
2. 更新PnP PowerShell模块至最新稳定版
旧版本PnP模块可能存在JSON序列化/反序列化的兼容bug,执行以下命令更新模块:
Update-Module -Name PnP.PowerShell -Force
更新完成后重新运行脚本测试。
3. 缩小模板导出范围定位问题
通过指定-Handlers参数缩小导出范围,先排除页面和Web部件相关内容,验证是否能成功导出:
$template = Get-PnPSiteTemplate -OutputInstance -Handlers SiteSettings
若成功,再逐步添加Handlers(如WebParts、Pages),定位到具体导致错误的内容模块。
4. 验证.NET SDK与PowerShell版本兼容性
确保安装的.NET SDK版本与当前PowerShell版本匹配:
- 执行
$PSVersionTable查看当前PowerShell版本 - PowerShell 7.x推荐搭配.NET 6及以上版本,可暂时卸载不兼容的旧版.NET SDK,避免版本冲突
内容的提问来源于stack exchange,提问作者Hade
相关产品推荐
相关产品推荐

