SharePoint Asked by Sylvie on February 17, 2021
I’d like to know if it’s possible to create a script to check a library for all guest access links (authenticated, not anonymous) and if they are older than 90 days, remove them.
So for each folder that was shared with an external user <90 days ago, remove link.
Here is a script to remove all links (tweaked from this post Remove a specific link from SharePoint Online):
Function Remove-SPSharingLink {
param (
$SPURL
)
process{
Connect-PnPOnline -Url $SPURL
$Ctx= Get-PnPContext
$Files= Get-PnPListItem -List "Documents"
foreach( $File in $Files)
{
$Froles= $File.RoleAssignments
$Ctx.load($Froles)
$Ctx.ExecuteQuery()
If($Froles.Count -gt 0)
{
for ($i = $Froles.Count -1; $i -ge 0 ; --$i)
{
$Link=$Froles[$i].Member
$Ctx.Load($Link)
$Ctx.ExecuteQuery()
If($Link.title -like "SharingLinks*")
{
$Froles[$i].DeleteObject()
}
$Link = $null
}
$Ctx.ExecuteQuery()
}
}
}
}
Remove-SPSharingLink -SPURL “https://MyTenant.sharepoint.com/sites/MySite“
But I’m not sure how to get the sharing link details and filter by date.
Thanks
There is no way, that I am aware of, to get the link shared date. You could store it as part of a script or app but that won't help for the first time you run it.
You can find guests using a search query and appending ViewableByExternalUsers=true. It is less expensive and quicker than what you are trying. So something like:
#This checks the last modified date. Not the link date...
#The important part is ViewableByExternalUsers:true
#You can figure this query out to suit you from a search box.
#This only returns docs. You may want folders etc.
$query = "Write>=$((Get-date).AddDays(-90).ToString("dd/MM/yyyy")) AND ViewableByExternalUsers:true AND IsDocument:true AND NOT contentclass:STS_ListItem_WebPageLibrary"
$keyWordQry = New-Object Microsoft.SharePoint.Client.Search.Query.KeywordQuery($ctx)
$keyWordQry.TrimDuplicates = $false
$keyWordQry.EnableStemming = $false
$keyWordQry.QueryText = $query
$searchExec = New-Object Microsoft.SharePoint.Client.Search.Query.SearchExecutor($ctx)
$results = $searchExec.ExecuteQuery($keyWordQry)
$ctx.ExecuteQuery()
At this point you have an object you can enumerate to get what you want:
$results.Value[0].ResultRows
Do note that this only returns the first 50 results by default. You can return more by setting $keyWordQry.RowLimit
and/or passing a start point to the query via $keyWordQry.StartRow
Also you would need to handle folder, library and site sharing. All possible with search though.
Answered by Bunzab on February 17, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP