[英]Unable to pass SharePoint (PnP) connection to PowerShell job because it's becoming deserialized?
I am trying to delete every file in a SharePoint list.我正在尝试删除 SharePoint 列表中的每个文件。 My org has retention turned on so I can't just delete the entire list, but must remove every folder/file.我的组织已启用保留功能,因此我不能只删除整个列表,而必须删除每个文件夹/文件。 My issue is around the connection itself when used with Start-Job
.我的问题是与Start-Job
一起使用时连接本身。
It's painfully slow, so I wanted to spin up batches of 10+ jobs to delete simultaneously and reuse the connection, but there is an issue passing the connection as an argument because it becomes deserialized.它非常慢,所以我想启动 10 个以上作业的批次以同时删除并重用连接,但是将连接作为参数传递存在问题,因为它被反序列化了。 I found this post with the exact same issue and no solution.我发现这篇文章有完全相同的问题,但没有解决方案。
If I "workaround" it by connecting each Start-Job
, I get throttled by SharePoint online.如果我通过连接每个Start-Job
来“解决”它,我会在网上受到SharePoint的限制。
function Empty-PnPFiles($SPSiteUrl, $RelativeURL)
{
$connection = Connect-PnPOnline -URL $SPSiteUrl -UseWebLogin -ReturnConnection
# Get All files in the folder
$Files = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType File
# Delete all files in the Folder
$n = 0
$Jobs = @()
ForEach ($File in $Files)
{
$n++
Write-Host "Creating job to delete '$($File.ServerRelativeURL)'"
#Delete File
$Jobs += Start-Job -ArgumentList @($connection, $File) -ScriptBlock {
$LocalConnection = $args[0]
# $LocalConnection = Connect-PnPOnline -URL <My SP URL> -UseWebLogin -ReturnConnection
$LocalFile = $args[1]
Remove-PnPFile -ServerRelativeUrl $LocalFile.ServerRelativeURL -Connection $LocalConnection -Force -Recycle
}
# Do in batches. Every 10 Jobs, wait for completion
if ($n % 10 -eq 0)
{
Write-Host "Waiting for batch $n ($($Files.Count)) to complete before next batch" -ForegroundColor Green
$Jobs | Wait-Job | Receive-Job
$Jobs = @()
}
}
# If there are left over jobs, wait for them
if ($Jobs)
{
$Jobs | Wait-Job | Receive-Job
}
}
$SiteURL = "https://<MySiteCollection>.sharepoint.com/sites/<MySite>"
$ListName = "TempDelete"
Empty-PnPFiles -SPSiteUrL $SiteURL -RelativeURL "/TempDelete" # <My Folder to Delete all files>
The error I get is:我得到的错误是:
Cannot bind parameter 'Connection'. Cannot convert the "PnP.PowerShell.Commands.Base.PnPConnection" value of type "Deserialized.PnP.PowerShell.Commands.Base.PnPConnection" to type "PnP.PowerShell.Commands.Base.PnPConnection".
How can I pass the connection to the script block without the serialization error?如何在没有序列化错误的情况下将连接传递给脚本块? Or is there a better way to bulk-delete files from SPO using PowerShell?或者是否有更好的方法使用 PowerShell 从 SPO 批量删除文件? I have to use PowerShell because it's the only tool available to me currently.我必须使用 PowerShell,因为它是我目前唯一可用的工具。
Use Invoke-Command instead of Start-Job使用 Invoke-Command 而不是 Start-Job
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.