繁体   English   中英

无法将 SharePoint (PnP) 连接传递给 PowerShell 作业,因为它正在反序列化?

[英]Unable to pass SharePoint (PnP) connection to PowerShell job because it's becoming deserialized?

我正在尝试删除 SharePoint 列表中的每个文件。 我的组织已启用保留功能,因此我不能只删除整个列表,而必须删除每个文件夹/文件。 我的问题是与Start-Job一起使用时连接本身。

它非常慢,所以我想启动 10 个以上作业的批次以同时删除并重用连接,但是将连接作为参数传递存在问题,因为它被反序列化了。 我发现这篇文章有完全相同的问题,但没有解决方案。

如果我通过连接每个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>

我得到的错误是:

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".

在此处输入图像描述

如何在没有序列化错误的情况下将连接传递给脚本块? 或者是否有更好的方法使用 PowerShell 从 SPO 批量删除文件? 我必须使用 PowerShell,因为它是我目前唯一可用的工具。

使用 Invoke-Command 而不是 Start-Job

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM