首页 > 解决方案 > format-default :集合尚未初始化。PowerShell + SharePoint Online 错误

问题描述

我的 PowerShell 脚本有问题。脚本从 SharePoint 列表中删除记录,然后从 csv 中添加新记录。基本上,当我逐行运行脚本时,一切正常。但是当我尝试运行整个事情时,我得到了这个错误:

format-default :集合尚未初始化。它尚未被请求或请求尚未执行。可能需要明确请求。+ CategoryInfo : NotSpecified: (:) [format-default], CollectionNotInitializedException + FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException,Microsoft.PowerShell.Commands.FormatDefaultCommand

这是我的代码:

$list_modified = 'list_name'
$filename = "C:\.......\file.csv"

# Purge existing records in list
$records = Get-PnPListItem -List $list_modified -PageSize 500

# make sure fields in list have corresponding field in csv and same data type
Get-PnPField -List $list_modified

# purge list
foreach ($record in $records)
{
    Write-Host "Removing Id "  $record.Id
    Remove-PnPListItem -List $list_modified -Identity $record.Id -Force
}

Get-PnPField -List $list_modified
$records = Get-PnPListItem -List $list_modified -PageSize 500
$records | measure

# Import csv
$csv_data = Import-CSV -Path $filename -Delimiter `t

foreach ($row in $csv_data) {
  Add-PnPListItem -List $list_modified -Values @{
  "Title" = $row.'name';
  "uuid" = $row.'uuid';
  "name" = $row.'name';
  "short_description" = $row.'short_description';
  "logo_url" = $row.'logo_url';
  "homepage_url" = $row.'homepage_url';
  "category_groups_list" = $row.'category_groups_list';
  "category_list" = $row.'category_list';
  "total_funding_usd" = $row.'total_funding_usd';
  "last_funding_on" = $row.'last_funding_on'
  }
}

我究竟做错了什么?请帮忙!

谢谢!马切伊

标签: powershellcsvsharepointsharepoint-online

解决方案


谷歌搜索以下内容会发现许多类似的问题:

format-default : 集合尚未初始化

来自:https ://github.com/pnp/PnP-PowerShell/issues/799#issuecomment-618926331

尝试将您的语句返回到变量中。

做这个

$item = Add-PnPListItem -List $list -ContentType "Project" -Values $Values**

$item = Set-PnPListItem -List $list -Identity $targetItem.Id -Values $Values**

推荐阅读