【发布时间】: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