SharePoint Online: PowerShell Kullanarak Word Belgelerindeki Bağlantılar Nasıl Güncelleştirilir?


Gereksinim: SharePoint Online belge kitaplığındaki word belgelerindeki bağlantıları güncelleyin.

PowerShell, Microsoft Word Belgelerindeki Köprüleri Güncelleştirecek

Bir geçiş projesinde, SharePoint Online belge kitaplıklarında depolanan Microsoft Word belgelerine gömülü tüm bağlantıları değiştirmek zorunda kaldık. Her belgeyi açmak ve eski bağlantıları yenileriyle güncellemek zahmetli olacaktır. Neyse ki, SharePoint Online belge kitaplığında depolanan tüm Microsoft Word belgelerindeki tüm köprüleri (ve bağlantı metnini) toplu olarak güncellemek için PowerShell'i kullanabiliriz.


#Variables
$DomainURL = "https://crescent.sharepoint.com"
$SiteURL = "https://crescent.sharepoint.com/sites/Funds"
$LibraryName = "Documents"
$OldLink = "National"
$NewLink = "Crescent"

Try
{
    #Connect to SharePoint Online
    Connect-PnPOnline -Url $SiteURL -UseWebLogin
    $Web = Get-PnPWeb

    #Create a new COM object for word
    $Word = New-Object -ComObject Word.Application
    $Word.Visible = $False

    #Get all Word documents from the library
    $Documents = Get-PnPListItem -List $LibraryName -PageSize 500 | Where {$_.FieldValues.FileRef -like "*.doc*"}

    ForEach($Document in $Documents)
    {
        Try
        {
            $DocumentPath = $DomainURL + $Document.FieldValues.FileRef
            Write-host -f Yellow "Processing Document:"$DocumentPath
            #Open the Document
            $WordDocument = $Word.documents.Open($DocumentPath)

            #Get all links from the document
            $Hyperlinks = @($WordDocument.Hyperlinks)

            #Search and Replace Old Links and Text
             $Hyperlinks | ForEach-Object {
                If ($_.Address.ToLower().Contains($OldLink.ToLower()) -or $_.TextToDisplay.ToLower().Contains($OldLink.ToLower()))
                {
                    #Get the current Link and Link Text
                    $CurrentLink = $_.Address
                    $CurrentText = $_.TextToDisplay

                    #Update Link and Description
                    $_.Address =  $_.Address -Replace $OldLink, $NewLink
                    $_.TextToDisplay = $_.TextToDisplay -Replace $OldLink, $NewLink
                    $WordDocument.save()
                    $Document.Update()
                    Write-host -f Green "`tLink '$CurrentText' at '$CurrentLink' has been updated!"
                } 
            }
        }
        Catch {
            write-host -f Red "Document Update Error:" $_.Exception.Message
        }
        Finally {
            $WordDocument.Close()
        }
    }
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}
Finally {
    $Word.quit()
}

Bu komut dosyası, bağlantının yanı sıra bağlantı metnini (açıklama) da değiştirecektir. Birden çok sitedeki tüm belge kitaplıklarını çağırmak için bunu bir işlevin içine sarabilirsiniz.

İlgili makaleler