Created
July 22, 2024 22:46
-
-
Save reshmee011/d54e69e40a1de64bfdf228b7a805ff58 to your computer and use it in GitHub Desktop.
Get Sharing Links combining CSOM and PNP
This file contains hidden or 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
#Parameters | |
$IsSite=$true | |
#$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($_ctx,$_object,$_type,$_siteUrl,$_listUrl) | |
{ | |
if( $_type -eq "File" -or $_type -eq "Item"){ | |
$links = Get-PnPFileSharingLink -FileUrl $_object.FieldValues["FileRef"] #more accurate info | |
$SharingInfo = [Microsoft.SharePoint.Client.ObjectSharingInformation]::GetObjectSharingInformation($_ctx, $_object, $false, $false, $false, $true, $true, $true, $true); | |
$_ctx.Load($SharingInfo) | |
$_ctx.ExecuteQuery() | |
# Assuming $links.Link is a collection of objects with WebUrl properties | |
$linkUrls = $links.Link | ForEach-Object { $_.WebUrl } # Extract WebUrls into a list | |
# Filter SharingLinks where Url is not in the extracted WebUrls | |
$link1s = $SharingInfo.SharingLinks | Where-Object { $linkUrls -notcontains $_.Url } | |
} | |
elseif( $_type -eq "Folder"){ | |
$links = Get-PnPFolderSharingLink -Folder $_object.FieldValues["FileRef"] | |
$SharingInfo = [Microsoft.SharePoint.Client.ObjectSharingInformation]::GetObjectSharingInformation($_ctx, $_object, $false, $false, $false, $true, $true, $true, $true); | |
$_ctx.Load($SharingInfo) | |
$_ctx.ExecuteQuery() | |
# Assuming $links.Link is a collection of objects with WebUrl properties | |
$linkUrls = $links.Link | ForEach-Object { $_.WebUrl } # Extract WebUrls into a list | |
# Filter SharingLinks where Url is not in the extracted WebUrls | |
$link1s = $SharingInfo.SharingLinks | Where-Object { $linkUrls -notcontains $_.Url } | |
} | |
ForEach($ShareLink in $links) | |
{ | |
$result = New-Object PSObject -property $([ordered]@{ | |
SiteUrl = $_SiteURL | |
listUrl = $_listUrl | |
Name = $_type -eq 'Item' ? $_object.FieldValues["Title"] : $_object.FieldValues["FileLeafRef"] | |
RelativeURL = $_object.FieldValues["FileRef"] | |
ObjectType = $_Type | |
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 | |
CSOMLink = 'No' | |
}) | |
$global:Results +=$result; | |
} | |
foreach($l in $link1s){ | |
if($l.Url){ | |
If($ShareLink.IsEditLink) | |
{ | |
$AccessType="Edit" | |
} | |
ElseIf($shareLink.IsReviewLink) | |
{ | |
$AccessType="Review" | |
} | |
Else | |
{ | |
$AccessType="ViewOnly" | |
} | |
$result = New-Object PSObject -property $([ordered]@{ | |
SiteUrl = $_SiteURL | |
listUrl = $_listUrl | |
Name = $_type -eq 'Item' ? $_object.FieldValues["Title"] : $_object.FieldValues["FileLeafRef"] | |
RelativeURL = $_object.FieldValues["FileRef"] | |
ObjectType = $_type | |
ShareId = $l.ShareId | |
RoleList = $null | |
Users = $null | |
ShareLinkUrl = $l.Url | |
ShareLinkAccess = $AccessType | |
ShareLinkType = $l.LinkKind | |
Expiration = $l.Expiration | |
BlocksDownload = $l.BlocksDownload | |
RequiresPassword = $l.RequiresPassword | |
CSOMLink = 'Yes' | |
}) | |
$global:Results +=$result; | |
} | |
} | |
} | |
#Exclude certain libraries | |
$ExcludedLists = @("Form Templates", "Preservation Hold Library", "Site Assets", "Images", "Pages", "Settings", "Videos","Timesheet" | |
"Site Collection Documents", "Site Collection Images", "Style Library", "AppPages", "Apps for SharePoint", "Apps for Office") | |
#$m365Sites = Get-PnPTenantSite| Where-Object { ( $_.Url -like '*sites/Contosoinc-intranet*') -and $_.Template -ne 'RedirectSite#0' } | |
#$m365Sites | ForEach-Object { | |
$siteUrl = Read-Host -Prompt "Enter site URL"; | |
Connect-PnPOnline -Url $siteUrl -Interactive | |
$ctx = Get-PnPContext | |
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) | |
{ | |
$listUrl = $list.RootFolder.ServerRelativeUrl; | |
#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) | |
{ | |
#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 $ctx $item $type $siteUrl $listUrl; | |
} | |
} | |
} | |
$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