Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save reshmee011/f9ff790bd6ff00824f30e8b46d39d6dc to your computer and use it in GitHub Desktop.
Save reshmee011/f9ff790bd6ff00824f30e8b46d39d6dc to your computer and use it in GitHub Desktop.
GetSharingLinks_PnP
#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,$_listUrl)
{
$relativeUrl = $_object.FieldValues["FileRef"]
$SharingLinks = if ($_type -eq "File") {
Get-PnPFileSharingLink -File $relativeUrl
} elseif ($_type -eq "Folder") {
Get-PnPFolderSharingLink -Folder $relativeUrl
}
ForEach($ShareLink in $SharingLinks)
{
Write-Host "Shared links found in '$relativeUrl'"
$ShareLink | Add-Member -NotePropertyName "SiteUrl" -NotePropertyValue $_siteUrl
$ShareLink | Add-Member -NotePropertyName "ListUrl" -NotePropertyValue $_listUrl
$ShareLink | Add-Member -NotePropertyName "ServerRelativeUrl" -NotePropertyValue $relativeUrl
$ShareLink | Add-Member -NotePropertyName "RoleList" -NotePropertyValue ($ShareLink.Roles -join "|")
$ShareLink | Add-Member -NotePropertyName "LinkUrl" -NotePropertyValue $ShareLink.Link.WebUrl
$ShareLink | Add-Member -NotePropertyName "LinkScope" -NotePropertyValue $ShareLink.Link.Scope
$ShareLink | Add-Member -NotePropertyName "LinkType" -NotePropertyValue $ShareLink.Link.Type
$ShareLink | Add-Member -NotePropertyName "LinkPreventsDownload" -NotePropertyValue $ShareLink.Link.PreventsDowload
$ShareLink | Add-Member -NotePropertyName "LinkUsers" -NotePropertyValue ($ShareLink.GrantedToIdentitiesV2.User.Email -join "|")
$global:Results +=$ShareLink;
}
}
#Exclude certain libraries
$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)
{
$listUrl = $list.RootFolder.ServerRelativeUrl;
#Get all list items in batches
$ListItems = Get-PnPListItem -List $list -PageSize 2000
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 $listUrl;
}
}
}
}
$global:Results | select Id,SiteUrl,ListUrl,ServerRelativeUrl,RoleList,LinkUrl,LinkScope,LinkType,LinkPreventsDownload,LinkUsers,Invitation,ShareId,ExpirationDateTime,HasPassword,HasChanges | ConvertTo-Csv -NoTypeInformation | Out-File $ReportOutput
#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