使用PnP.PowerShell的Get-PnPFile Cmdlet时遭遇初始化异常及连接不稳定问题求助
我在使用PnP PowerShell的Get-PnPFile Cmdlet时遇到间歇性报错,错误信息如下:
Get-PnPFile : The property or field 'ServerRelativeUrl' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
At line:1 char:1
Get-PnPFile -Url $fileURL -Path $downloadPath -Filename Leavers.CSV - ...
- CategoryInfo : WriteError: (:) [Get-PnPFile], PropertyOrFieldNotInitializedException
- FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.GetFile
我的相关代码如下:
$siteURL = "https://contoso.sharepoint.com/sites/ExampleSite" $fileURL = "/sites/ExampleSite/Path/To/The/File.csv" $downloadPath = "C:\\Temp" Connect-PnPOnline -Url $siteURL -Credentials (Get-Credential) Get-PnPFile -Url $fileURL -Path $downloadPath -Filename File.CSV -AsFile
奇怪的是这段代码之前能正常运行,现在却间歇性成功,多数时候报错,且找不到规律。Connect-PnPOnline执行无报错,但多数时候连接状态异常:用Get-PnPSite检查站点连接时,大部分情况下返回空的站点信息:
PS C:\\Windows\\system32> Get-PNPsite Url CompatibilityLevel --- ------------------
只有少数Get-PnPFile成功的情况下,Get-PnPSite才会正确返回字段值。
我已经尝试过以下方案但无效:
- 将
Connect-PnPOnline的结果绑定到变量再执行Get-PnPFile:$connection = Connect-PnPOnline -Url $siteURL -Credentials (Get-Credential) $file = Get-PnPFile -Url $fileURL -Path $downloadPath -Filename File.CSV -AsFile - 升级/降级PnP.PowerShell版本(从1.8.0→1.8.11→1.7.0)
这种间歇性的连接属性未初始化问题,通常和PnP连接上下文的缓存、属性请求逻辑有关,我整理了几个经过验证的解决方法,你可以逐一尝试:
1. 显式指定连接对象并请求所需属性
很多时候连接上下文没有被正确传递,或者默认没有请求ServerRelativeUrl这个属性,导致后续命令报错。我们可以通过-ReturnConnection获取明确的连接对象,再手动指定要请求的属性:
$siteURL = "https://contoso.sharepoint.com/sites/ExampleSite" $fileURL = "/sites/ExampleSite/Path/To/The/File.csv" $downloadPath = "C:\\Temp" # 获取连接对象,确保上下文正确 $connection = Connect-PnPOnline -Url $siteURL -Credentials (Get-Credential) -ReturnConnection # 显式指定需要的属性并传递连接对象 Get-PnPFile -Url $fileURL -Path $downloadPath -Filename File.CSV -AsFile -Connection $connection -Properties "ServerRelativeUrl"
这样能强制命令使用我们确认有效的连接,并且确保所需的属性被提前请求初始化。
2. 切换认证方式并清除凭据缓存
传统凭据认证可能存在会话缓存失效的问题,你可以尝试切换到现代交互式认证测试:
Connect-PnPOnline -Url $siteURL -Interactive
如果还是有问题,可以清除PowerShell的PnP连接缓存和系统凭据管理器中的相关条目:
# 清除所有PnP连接缓存 Disconnect-PnPOnline -All # 打开系统凭据管理器删除SharePoint相关凭据 rundll32.exe keymgr.dll,KRShowKeyMgr
删除旧凭据后重新连接,能避免缓存的无效凭据干扰连接。
3. 添加连接验证与重试逻辑
在执行Get-PnPFile前先验证连接是否有效,若无效则自动重试,避免在连接异常的情况下执行后续命令:
$siteURL = "https://contoso.sharepoint.com/sites/ExampleSite" $fileURL = "/sites/ExampleSite/Path/To/The/File.csv" $downloadPath = "C:\\Temp" $maxRetries = 3 $retryCount = 0 $connectionValid = $false while (-not $connectionValid -and $retryCount -lt $maxRetries) { try { $connection = Connect-PnPOnline -Url $siteURL -Credentials (Get-Credential) -ReturnConnection -ErrorAction Stop # 用Get-PnPSite验证连接有效性 $site = Get-PnPSite -Connection $connection -ErrorAction Stop if ($site.Url) { $connectionValid = $true Write-Host "连接验证成功" } } catch { $retryCount++ Write-Host "连接验证失败,正在重试第$retryCount次..." Start-Sleep -Seconds 5 } } if ($connectionValid) { Get-PnPFile -Url $fileURL -Path $downloadPath -Filename File.CSV -AsFile -Connection $connection } else { Write-Error "多次重试后仍无法建立有效连接" }
4. 重新安装PnP PowerShell模块
有时候模块文件损坏也会导致上下文异常,你可以彻底卸载后重新安装一个稳定的新版本:
# 卸载所有版本的PnP模块 Uninstall-Module PnP.PowerShell -AllVersions -Force # 安装指定稳定版本(比如1.9.0,可根据实际情况调整) Install-Module PnP.PowerShell -RequiredVersion 1.9.0 -Force -AllowClobber
安装完成后记得重新打开PowerShell窗口再执行脚本。
内容的提问来源于stack exchange,提问作者apaaaan

