【问题标题】:format-default : The collection has not been initialized. PowerShell + SharePoint Online Errorformat-default :集合尚未初始化。 PowerShell + SharePoint Online 错误
【发布时间】:2021-01-10 20:40:48
【问题描述】:

我的 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'
  }
}

我做错了什么?请帮忙!

谢谢! 马切耶

【问题讨论】:

  • 您是否考虑过将您的问题作为 pnp powershell cmdlet 的 github 页面上的问题发布?他们也许可以帮助您解决问题:github.com/pnp/PnP-PowerShell
  • 另外,我不确定这是否会产生很大的影响,但是关于您在“add-pnplistitem”cmdlet 中编写哈希表的方式,您可以尝试以不同的方式格式化它吗?尝试像这样格式化它: Title = "$($row.Name)" #排除所有“;”,因为新行将分隔值
  • 为了让我们更好地帮助您,了解脚本中发生错误的位置也将有所帮助。考虑在您的脚本中放置几个​​ Write-host "" 函数来识别错误发生的位置,以便我们可以隔离引发错误的原因。
  • 感谢@WilliamHiggs!它在第 28 行崩溃: Add-PnPListItem -List $list_modified -Values

标签: powershell csv sharepoint sharepoint-online


【解决方案1】:

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

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**

【讨论】:

  • 谢谢@Matthew!你能帮忙把它实现到代码中吗?我是 PowerShell 新手,不知道如何使用它。在下面的评论中尝试了类似的东西,但效果相同:
  • $item = Add-PnPListItem -List $list_modified -ContentType "Project" -Values @{ "uuid" = $row.'uuid'; "news_company" = $row.'news_company'; "Title" = $row.'title'; "posted_on" = $row.'posted_on'; "logo_url" = $row.'logo_url'; "url" = $row.'url'; "source" = $row.'Source' } foreach ($row in $csv_data) {$item}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-01
  • 2016-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-25
相关资源
最近更新 更多