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
1 Asked on October 25, 2021 by marko-farkas
1 Asked on October 25, 2021 by sree-kuttan
1 Asked on October 25, 2021 by lukas-liuas
1 Asked on October 25, 2021 by user90269
1 Asked on October 25, 2021
0 Asked on October 25, 2021 by spdevadmin
1 Asked on October 25, 2021 by newone
1 Asked on October 25, 2021
1 Asked on October 25, 2021
2 Asked on March 8, 2021
1 Asked on February 26, 2021 by i-junior
3 Asked on February 25, 2021 by taruki_ms
audience targeting modern experience office 365 quick links sharepoint online
2 Asked on February 24, 2021
1 Asked on February 21, 2021 by maanoj-kumar
microsoft forms office 365 onedrive for business power automate sharepoint online
1 Asked on February 21, 2021
2 Asked on February 19, 2021 by user1258
2 Asked on February 18, 2021 by kmo
2 Asked on February 18, 2021 by hushhoney
Get help from others!
Recent Questions
Recent Answers
© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP