-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #795 from reshmee011/deletenondirectsharinglink
sample script to delete company wide and anonymous link
- Loading branch information
Showing
3 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
145 changes: 145 additions & 0 deletions
145
scripts/spo-delete-companywide-anonymous-sharinglink/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
--- | ||
plugin: add-to-gallery | ||
--- | ||
|
||
# Deletes company-wide and anonymous sharing links | ||
|
||
## Summary | ||
|
||
Sharing links can lead to oversharing, especially when default site sharing settings haven’t been updated to ‘People with existing access’. If default sharing options are not updated in the tenant or site, end users can easily create a company wide or anonymous sharing links with a single click. End users can make a conscious decision to create sharing links with specific "people you choose" which limits the audience having access to the data. This script can help delete those company-wide and anonymous sharing links at folder, file, and item levels. This approach can help mitigate oversharing issues during the M365 Copilot rollout. | ||
|
||
![Example Screenshot](assets/preview.png) | ||
|
||
### Prerequisites | ||
|
||
- The user account that runs the script must have at least site owner role to the SharePoint Online site. | ||
|
||
# [PnP PowerShell](#tab/pnpps) | ||
|
||
```powershell | ||
param( | ||
[Parameter(Mandatory)] | ||
[ValidateSet('Yes','No')] | ||
[string]$ExcludeDirectSharingLink,#anonymous and company wide links are included by default and this provides an option to include the "People You choose" links in the deletio process. | ||
[Parameter(Mandatory)] | ||
[ValidateSet('Yes','No')] | ||
[string]$DeleteSharingink, | ||
[Parameter(Mandatory)] | ||
[string]$SiteUrl | ||
) | ||
#Parameters | ||
$dateTime = (Get-Date).toString("dd-MM-yyyy-hh-ss") | ||
$invocation = (Get-Variable MyInvocation).Value | ||
$directorypath = Split-Path $invocation.MyCommand.Path | ||
$fileName = "SharedLinksDeletion-" + $dateTime + ".csv" | ||
$ReportOutput = $directorypath + "\Logs\"+ $fileName | ||
$global:Results = @(); | ||
$siteUrl = if ($siteUrl[-1] -ne '/') { $siteUrl + '/' } else { $siteUrl } | ||
function getSharingLink($_object,$_type,$_siteUrl,$_listUrl) | ||
{ | ||
$relativeUrl = $_object.FileRef | ||
$SharingLinks = if ($_type -eq 0 ) { | ||
Get-PnPFileSharingLink -Identity $relativeUrl | ||
} elseif ($_type -eq 1) { | ||
Get-PnPFolderSharingLink -Folder $relativeUrl | ||
} | ||
ForEach($ShareLink in $SharingLinks) | ||
{ | ||
if(($ExcludeDirectSharingLink -eq 'Yes' -and $ShareLink.Link.Scope -ne 'Users') -or $ExcludeDirectSharingLink -eq 'No') | ||
{ | ||
$result = New-Object PSObject -property $([ordered]@{ | ||
SiteUrl = $_SiteURL | ||
listUrl = $_listUrl | ||
Name = $_object.FileLeafRef | ||
RelativeURL = $_object.FileRef | ||
ObjectType = $_Type -eq 1 ? 'Folder':'File' | ||
ShareId = $ShareLink.Id | ||
RoleList = $ShareLink.Roles -join "|" | ||
Users = $ShareLink.GrantedToIdentitiesV2.User.Email -join "|" | ||
ShareLinkUrl = $ShareLink.Link.WebUrl | ||
ShareLinkType = $ShareLink.Link.Type | ||
ShareLinkScope = $ShareLink.Link.Scope | ||
Expiration = $ShareLink.ExpirationDateTime | ||
BlocksDownload = $ShareLink.Link.PreventsDowload | ||
RequiresPassword = $ShareLink.HasPassword | ||
}) | ||
$global:Results +=$result; | ||
if($DeleteSharingink -eq 'Yes'){ | ||
if ($_type -eq 0 ) { | ||
Remove-PnPFileSharingLink -FileUrl $relativeUrl -Identity $ShareLink.Id -Force | ||
} elseif ($_type -eq 1) { | ||
Remove-PnPFolderSharingLink -Folder $relativeUrl -Identity $ShareLink.Id -Force | ||
} | ||
} | ||
} | ||
} | ||
} | ||
#Exclude certain libraries/lists | ||
$ExcludedLists = @("Access Requests", "App Packages", "appdata", "appfiles","Apps for SharePoint" ,"Apps in Testing", "Cache Profiles", "Composed Looks", "Content and Structure Reports", "Content type publishing error log", "Converted Forms", | ||
"Device Channels", "Form Templates", "fpdatasources", "Get started with Apps for Office and SharePoint", "List Template Gallery", "Long Running Operation Status", "Maintenance Log Library", "Images", "site collection images" | ||
, "Master Docs", "Master Page Gallery", "MicroFeed", "NintexFormXml", "Quick Deploy Items", "Relationships List", "Reusable Content", "Reporting Metadata", "Reporting Templates", "Search Config List", "Site Assets", "Preservation Hold Library", | ||
"Site Pages", "Solution Gallery", "Style Library", "Suggested Content Browser Locations", "Theme Gallery", "TaxonomyHiddenList", "User Information List", "Web Part Gallery", "wfpub", "wfsvc", "Workflow History", "Workflow Tasks", "Pages") | ||
Connect-PnPOnline -Url $siteUrl -Interactive | ||
Write-Host "Processing site $siteUrl" -Foregroundcolor "Red"; | ||
$ll = Get-PnPList -Includes BaseType, Hidden, Title,HasUniqueRoleAssignments,RootFolder | Where-Object {$_.Hidden -eq $False -and $_.Title -notin $ExcludedLists } #$_.BaseType -eq "DocumentLibrary" | ||
Write-Host "Number of lists $($ll.Count)"; | ||
foreach($list in $ll) | ||
{ | ||
$listUrl = $list.RootFolder.ServerRelativeUrl; | ||
$selectFields = "ID,HasUniqueRoleAssignments,FileRef,FileLeafRef,FileSystemObjectType" | ||
$Url = $siteUrl + '_api/web/lists/getbytitle(''' + $($list.Title) + ''')/items?$select=' + $($selectFields) | ||
$nextLink = $Url | ||
$ListItems = @() | ||
while($nextLink){ | ||
$response = invoke-pnpsprestmethod -Url $nextLink -Method Get | ||
$ListItems += $response.value | where-object{$_.HasUniqueRoleAssignments -eq $true} | ||
if($response.'odata.nextlink'){ | ||
$nextLink = $response.'odata.nextlink' -replace "$siteUrl/_api/","" | ||
} else{ | ||
$nextLink = $null | ||
} | ||
} | ||
ForEach($item in $ListItems) | ||
{ | ||
if($list.BaseType -eq "DocumentLibrary") | ||
{ | ||
$type= $item.FileSystemObjectType; | ||
} | ||
getSharingLink $item $type $siteUrl $listUrl; | ||
} | ||
} | ||
$global:Results | Export-CSV $ReportOutput -NoTypeInformation | ||
Write-host -f Green "Sharing Links Report Generated Successfully! to $ReportOutput" | ||
``` | ||
|
||
[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)] | ||
|
||
*** | ||
|
||
## Source Credit | ||
|
||
Sample first appeared on [Deletion of company-wide and anonymous sharing links with PowerShell](https://reshmeeauckloo.com/posts/powershell-delete-nondirectlink//) | ||
|
||
## Contributors | ||
|
||
| Author(s) | | ||
|-----------| | ||
| [Reshmee Auckloo](https://github.com/reshmee011) | | ||
|
||
|
||
[!INCLUDE [DISCLAIMER](../../docfx/includes/DISCLAIMER.md)] | ||
<img src="https://m365-visitor-stats.azurewebsites.net/script-samples/scripts/spo-delete-companywide-anonymous-sharinglink" aria-hidden="true" /> |
Binary file added
BIN
+96.7 KB
scripts/spo-delete-companywide-anonymous-sharinglink/assets/preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 62 additions & 0 deletions
62
scripts/spo-delete-companywide-anonymous-sharinglink/assets/sample.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
[ | ||
{ | ||
"name": "spo-delete-companywide-anonymous-sharinglink", | ||
"source": "pnp", | ||
"title": "Deletes company-wide and anonymous sharing links", | ||
"shortDescription": "Deletes company-wide and anonymous sharing links for folder, file, and item. This can address oversharing issues during the M365 Copilot rollout.", | ||
"url": "https://pnp.github.io/script-samples/spo-delete-companywide-anonymous-sharinglink/README.html", | ||
"longDescription": [ | ||
"Sharing links can lead to oversharing, especially when default site sharing settings haven't been updated to 'People with existing access'. If default sharing options are not updated in the tenant or site, end users can easily create a company wide or anonymous sharing links with a single click. End users can make a conscious decision to create sharing links with specific 'people you choose' which limits the audience having access to the data. This script can help delete those company-wide and anonymous sharing links at folder, file, and item levels. This approach can help mitigate oversharing issues during the M365 Copilot rollout." | ||
], | ||
"creationDateTime": "2025-01-03", | ||
"updateDateTime": "2025-01-03", | ||
"products": [ | ||
"SharePoint", | ||
"Sharing Links", | ||
"Microsoft 365 Copilot" | ||
], | ||
"metadata": [ | ||
{ | ||
"key": "PNP-POWERSHELL", | ||
"value": "2.99.90" | ||
} | ||
], | ||
"categories": [ | ||
"Report", | ||
"Microsoft 365 Copilot" | ||
], | ||
"tags": [ | ||
"modern", | ||
"Connect-PnPOnline", | ||
"Get-PnPFileSharingLink", | ||
"Get-PnPFolderSharingLink", | ||
"Get-PnPList", | ||
"Remove-PnPFileSharingLink", | ||
"Remove-PnPFolderSharingLink", | ||
"invoke-pnpsprestmethod" | ||
], | ||
"thumbnails": [ | ||
{ | ||
"type": "image", | ||
"order": 100, | ||
"url": "https://raw.githubusercontent.com/pnp/script-samples/main/scripts/spo-delete-companywide-anonymous-sharinglink/assets/preview.png", | ||
"alt": "" | ||
} | ||
], | ||
"authors": [ | ||
{ | ||
"gitHubAccount": "reshmee011", | ||
"company": "", | ||
"pictureUrl": "https://avatars.githubusercontent.com/u/7693852?v=4", | ||
"name": "Reshmee Auckloo" | ||
} | ||
], | ||
"references": [ | ||
{ | ||
"name": "Want to learn more about PnP PowerShell and the cmdlets", | ||
"description": "Check out the PnP PowerShell site to get started and for the reference to the cmdlets.", | ||
"url": "https://aka.ms/pnp/powershell" | ||
} | ||
] | ||
} | ||
] |