You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PnP PowerShell的Get-PnPPage cmdlet时遇空引用异常

解决PnP.PowerShell中Get-PnPPage的空引用异常

问题描述

使用PnP.PowerShell模块时,多数操作正常,但执行以下命令获取页面时触发空引用异常:

Get-PnPPage -Identity "Pages/home.aspx"

返回异常信息:

ScriptLineNumber : 1

Message          : Object reference not set to an instance of an object.
Stacktrace       :    at PnP.PowerShell.Commands.Base.PnPConnectedCmdlet.ProcessRecord() in
                   D:\a\powershell\powershell\src\Commands\Base\PnPConnectedCmdlet.cs:line 103
                      at PnP.PowerShell.Commands.PnPSharePointCmdlet.ProcessRecord() in D:\a\powershell\powershell\src\Commands\Base\PnPSharePointCmdlet.cs:line
                   132
                      at System.Management.Automation.CommandProcessor.ProcessRecord()
ScriptLineNumber : 15

注:报错时连接仍有效,后续可正常枚举子网站列表。

排查与解决方案

1. 验证页面的存在性与类型

  • 先确认目标页面是否存在于Pages库中,且为Modern页面(Get-PnPPage仅支持Modern页面,经典页面需用Get-PnPFile等命令):
    Get-PnPListItem -List "Pages"
    
  • 若需精准查询,可使用CAML过滤:
    Get-PnPListItem -List "Pages" -Query "<View><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>home.aspx</Value></Eq></Where></Query></View>"
    

2. 修正-Identity参数格式

Get-PnPPage的-Identity参数默认指向Pages库,无需完整路径或.aspx后缀,简化命令:

Get-PnPPage -Identity "home"

3. 升级PnP.PowerShell至最新版本

旧版本模块可能存在空引用bug,执行升级命令:

Update-Module -Name PnP.PowerShell -Force

升级后重新尝试命令。

4. 确认当前连接的站点正确性

若存在多站点环境,确保当前连接的是目标页面所在的站点,或在命令中显式指定连接:

$conn = Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/targetsite" -Interactive -ReturnConnection
Get-PnPPage -Identity "home" -Connection $conn

5. 检查页面状态

确认页面未处于草稿、回收站或已删除状态,可通过SharePoint站点直接访问页面验证可用性。

内容的提问来源于stack exchange,提问作者user3655752

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 00:01:03