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.
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)
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.