Created
May 6, 2024 09:14
-
-
Save reshmee011/24bf63cc81ccae604b563a7c312f001f to your computer and use it in GitHub Desktop.
getsharinginformation_csom
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
#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($_ctx,$_object,$_type,$_siteUrl,$_listUrl) | |
{ | |
$SharingInfo = [Microsoft.SharePoint.Client.ObjectSharingInformation]::GetObjectSharingInformation($_ctx, $_object, $false, $false, $false, $true, $true, $true, $true) | |
$ctx.Load($SharingInfo) | |
$ctx.ExecuteQuery() | |
ForEach($ShareLink in $SharingInfo.SharingLinks) | |
{ | |
If($ShareLink.Url) | |
{ | |
If($ShareLink.IsEditLink) | |
{ | |
$AccessType="Edit" | |
} | |
ElseIf($shareLink.IsReviewLink) | |
{ | |
$AccessType="Review" | |
} | |
Else | |
{ | |
$AccessType="ViewOnly" | |
} | |
#Collect the data | |
if($_type -eq "File") | |
{ | |
$ObjectType = $_object.FileSystemObjectType; | |
$Name = $_object.FieldValues["FileLeafRef"] | |
$RelativeURL = $_object.FieldValues["FileRef"] | |
} | |
else | |
{ | |
$ObjectType = $_type; | |
$Name = ""; | |
$RelativeURL = $listUrl ?? $SiteUrl; | |
} | |
$result = New-Object PSObject -property $([ordered]@{ | |
SiteUrl = $_siteURL | |
listUrl = $_listUrl | |
Name = $Name | |
RelativeURL = $RelativeURL | |
ObjectType = $ObjectType | |
ShareLink = $ShareLink.Url | |
ShareLinkAccess = $AccessType | |
HasExternalGuestInvitees = $ShareLink.HasExternalGuestInvitees | |
ShareLinkType = $ShareLink.LinkKind | |
AllowsAnonymousAccess = $ShareLink.AllowsAnonymousAccess | |
IsActive = $ShareLink.IsActive | |
Expiration = $ShareLink.Expiration | |
}) | |
$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/*') -and $_.Template -ne 'RedirectSite#0' } | |
$m365Sites | ForEach-Object { | |
$siteUrl = $_.Url; | |
Connect-PnPOnline -Url $siteUrl -Interactive | |
$ctx = Get-PnPContext | |
$web= Get-PnPWeb | |
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) | |
{ | |
$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= "File"; | |
} | |
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