Powershell脚本可编辑SharePoint Online的文档库中保存的多个文档中的链接

时间:2019-10-11 13:39:42

标签: powershell sharepoint sharepoint-online

我正在尝试编写PowerShell脚本,使我能够遍历SharePoint Online文档库中的每个文档,并更改每个文档中的嵌入式链接。

我尝试过类似于herehere的代码,但都没有效果

到目前为止,我有-

add-type -AssemblyName “Microsoft.Office.Interop.Word”
$wdunits = “Microsoft.Office.Interop.Word.wdunits” -as [type]
$donotsave = “Microsoft.Office.Interop.Word.wdDoNotSaveChanges” -as [type]
$save = “Microsoft.Office.Interop.Word.wdSaveChanges” -as [type]
$application = New-Object -ComObject word.application
$application.Visible = $false

Connect-PnPOnline -Url $sharepointSite -Credentials get-credentials

$lib = Get-PnPList -Identity "Test"

$libitems = Get-PnPListItem -List $lib

foreach($libitem in $libitems)
    {
        write-host "File Name: "$libitem["FileLeafRef"]

        if({$_.Name -match ".doc*"}) 
        {
            Write-Host "Found doc"
            $DocumentLocation = -join ( "SharePoint         Tenancy",$libitem["FileRef"])
            Write-Host $DocumentLocation

            $document = $application.Documents.Open($DocumentLocation)

            $DocURLs = $document.Hyperlinks

             foreach ($URL in $DocURLs | where {$_.Address -match     "oldLink"})
            {
                write-host "Found an old URL... "$URL.Address
                $NewURLString = $URL.Address 
                $NewURLString = $NewURLString -replace "oldLink", "newLink"
                $URL.Address = $NewURLString
                write-host "URL is now ... " $URL.Address

                $document.save()
                $document.close([Ref]$save)
                $libitem.file.Update()

            write-host "File Updated"
            }
        }
    }

$application.quit() 

它应该将库中所有文档中的旧链接更改为新链接,并在控制台窗口中显示一条消息,指出已找到并已更新旧链接。

但是什么都没出现,控制台上只显示FileLeafRef和$ documentLocation,根本没有链接被更新。

0 个答案:

没有答案