'How do find broken document links in SharePoint libraries

We are trying to find all broken document links in a SharePoint document library. These links are stored in libraries as .url files. The file itself contains the link we want to test. Using powershell, we submit the link to invoke-webrequest. But we always get a 200 (page found) result, even for known broken links. When we look at the response package, we see that SharePoint is returning the "Login page", which means we most likely do not have permission to access the link via invoke-webrequest. We are connecting using -WebLogin, not -Interactive.

    $text = Get-PnPFile -Url $Item.FileRef -AsString # get content of the .url file
    # parse file to get target link as $link_url
    $result = (invoke-webrequest $link_url - DisableKeepAlive -UseBasicParsing)

always returns

StatusCode :200
StatusDescription:OK
Content: ...

Always returns sign-in page.



Solution 1:[1]

How you tried running the script using the credentials option?

$text = Get-PnPFile -Url $Item.FileRef -AsString # get content of the .url file
# parse file to get target link as $link_url
$result = (invoke-webrequest $link_url - DisableKeepAlive -UseBasicParsing -UseDefaultCredentials)

Solution 2:[2]

This solution works for me. Note that you will have to go to your browser to complete the login:

   Connect-PnPOnline  -Url $site_url -Interactive

   #Get all the items in this library
   $Items = (Get-PnPListItem -List $list_name  -PageSize 2500).FieldValues    

   foreach($Item in $Items)
   {
      if (($Item.FileRef).Contains(".url")){
        # this is a document link... do something with it.
      } 
      else{ 
        # this is a document, so do document things with it.,
      }
   } # for-each item in the library 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dennis
Solution 2 Donald Koscheka