Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save reshmee011/be60dcf4e73c250aa408441423835c62 to your computer and use it in GitHub Desktop.
Save reshmee011/be60dcf4e73c250aa408441423835c62 to your computer and use it in GitHub Desktop.
GetSharingLinks_REST
#Parameters
$tenantUrl = Read-Host -Prompt "Enter tenant collection URL";
$dateTime = (Get-Date).toString("dd-MM-yyyy-hh-ss")
$invocation = (Get-Variable MyInvocation).Value
$directorypath = Split-Path $invocation.MyCommand.Path
$fileName = "SharedLinks-" + $dateTime + ".csv"
$ReportOutput = $directorypath + "\Logs\"+ $fileName
#Connect to PnP Online
Connect-PnPOnline -Url $tenantUrl -Interactive
$global:Results = @();
function getSharingLink($_object,$_type,$_siteUrl,$_list)
{
$relativeUrl = $_object.FieldValues["FileRef"]
$sharingSettings = Invoke-PnPSPRestMethod -Method Post -Url "$($_siteUrl)/_api/web/Lists(@a1)/GetItemById(@a2)/GetSharingInformation?@a1='{$($_list.Id)}'&@a2='$($_object.Id)'&`$Expand=permissionsInformation,pickerSettings" -ContentType "application/json;odata=verbose" -Content "{}"
ForEach ($shareLink in $sharingSettings.permissionsInformation.links)
{
Write-Host "Shared links found in '$relativeUrl'"
$linkDetails = $shareLink.linkDetails
if ($linkDetails.Url) {
$invitees = (
$linkDetails.Invitations |
ForEach-Object { $_.Invitee.email }
) -join '|'
$LastModified = Get-Date -Date $linkDetails.LastModified
$linkAccess = "ViewOnly"
if ($linkDetails.IsEditLink) {
$linkAccess = "Edit"
}
elseif ($linkDetails.IsReviewLink) {
$linkAccess = "Review"
}
$sharedLinkObject = New-Object PSObject -property $([ordered]@{
ItemID = $item.FieldValues["UniqueId"]
ShareLink = $linkDetails.Url
Invitees = $invitees
Name = $_object.FieldValues["FileLeafRef"] ?? $_object.Title
FileType = $_object.FieldValues["File_x0020_Type"] ?? "Item"
RelativeURL = $_object.FieldValues["FileRef"] ?? ""
LinkAccess = $linkAccess
LastModified = $LastModified
# Add other properties as needed
})
$global:Results +=$sharedLinkObject;
}
}
}
#Exclude system lists
$ExcludedLists = @("Access Requests", "App Packages", "appdata", "appfiles", "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")
$m365Sites = Get-PnPTenantSite| Where-Object { ( $_.Url -like '*/sites/*') -and $_.Template -ne 'RedirectSite#0' }
$m365Sites | ForEach-Object {
$siteUrl = $_.Url;
Connect-PnPOnline -Url $siteUrl -Interactive
Write-Host "Processing site $siteUrl" -Foregroundcolor "Red";
#getSharingLink $ctx $web "site" $siteUrl "";
$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)
{
#Get all list items in batches
$ListItems = Get-PnPListItem -List $list -PageSize 2000
# getSharingLink $ctx $list "list/library" $siteUrl $listUrl;
#Iterate through each list item
ForEach($item in $ListItems)
{
$ItemCount = $ListItems.Count
#Check if the Item has unique permissions
$HasUniquePermissions = Get-PnPProperty -ClientObject $Item -Property "HasUniqueRoleAssignments"
If($HasUniquePermissions)
{
#Get Shared Links
if($list.BaseType -eq "DocumentLibrary")
{
$type= $item.FileSystemObjectType;
}
else
{
$type= "Item";
}
getSharingLink $item $type $siteUrl $list;
}
}
}
}
$global:Results | Export-CSV $ReportOutput -NoTypeInformation
Write-host -f Green "Sharing Links Report Generated Successfully!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment