Last active
April 18, 2024 18:28
-
-
Save joerodgers/b14b85d68c53640d639764b51579e02d to your computer and use it in GitHub Desktop.
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
#requires -modules "Microsoft.Graph.Authentication", "Microsoft.Graph.Sites", "Microsoft.Graph.Files" | |
function Get-SiteDriveItem | |
{ | |
[CmdletBinding(DefaultParameterSetName="root")] | |
param | |
( | |
[Parameter(Mandatory=$true,ParameterSetName="root")] | |
[Parameter(Mandatory=$true,ParameterSetName="item")] | |
[string] | |
$DriveId, | |
[Parameter(Mandatory=$true,ParameterSetName="item")] | |
[string] | |
$ItemId | |
) | |
begin | |
{ | |
} | |
process | |
{ | |
if( $PSCmdlet.ParameterSetName -eq "root" ) | |
{ | |
$driveRoot = Get-MgDriveRoot -DriveId $DriveId | |
$ItemId = $driveRoot.id | |
} | |
$children = Get-MgDriveItemChild -DriveId $DriveId -DriveItemId $ItemId | |
foreach( $driveItem in $children ) | |
{ | |
$driveItem | |
if( $driveItem.folder.childcount -gt 0 ) | |
{ | |
Get-SiteDriveItem -DriveId $DriveId -ItemId $driveItem.Id | |
} | |
} | |
} | |
end | |
{ | |
} | |
} | |
function Get-DriveItemSharingLink | |
{ | |
[CmdletBinding()] | |
param | |
( | |
[Parameter(Mandatory=$true)] | |
[string] | |
$SiteId | |
) | |
begin | |
{ | |
$site = Get-MgSite -SiteId $SiteId | |
$drives = Get-MgSiteDrive -SiteId $site.Id | |
} | |
process | |
{ | |
foreach( $drive in $drives ) | |
{ | |
# recursively get all files/folders in the drive | |
$driveItems = @(Get-SiteDriveItem -DriveId $drive.Id) | |
Write-Verbose "[$(Get-Date)] - Discovered $($driveItems.Count) drives." | |
# remove items w/o any sharing links | |
$driveItems = $driveItems | Where-Object -Property Shared -ne $null | |
# enumerate drive items | |
foreach( $driveItem in $driveItems ) | |
{ | |
Write-Verbose "[$(Get-Date)] - Processsing: $($driveItem.WebUrl)" | |
# get drive permissions | |
$permissions = @(Get-MgDriveItemPermission -DriveId $drive.Id -DriveItemId $driveItem.Id) | |
Write-Verbose "[$(Get-Date)] - Discovered: $($permissions.Count)" | |
# enumerate permissions | |
foreach( $permission in $permissions ) | |
{ | |
if( $null -ne $permission.Link.Scope ) | |
{ | |
$sharedWith = $permission.grantedToIdentitiesV2.SiteUser.LoginName -join "," | |
$sharedWith = $sharedWith -replace "i:0#\.f\|membership\|", "" | |
# build the sharing link details | |
[PSCustomObject] @{ | |
SiteUrl = $site.webUrl | |
DriveUrl = $drive.webUrl | |
ShareId = $permission.ShareId | |
LinkUrl = $permission.Link.WebUrl | |
LinkPreventsDownload = $permission.Link.PreventsDownload | |
LinkHasPassword = $permission.HasPassword | |
LinkExpirationDateTime = $permission.ExpirationDateTime | |
LinkScope = [System.Globalization.CultureInfo]::CurrentCulture.TextInfo.ToTitleCase( $permission.Link.Scope ) | |
LinkSharedWith = $sharedWith | |
LinkRole = [System.Globalization.CultureInfo]::CurrentCulture.TextInfo.ToTitleCase( $permission.Roles[0] ) | |
ItemName = $driveItem.Name | |
ItemUrl = $driveItem.WebUrl | |
} | |
} | |
elseif( $permission.Invitation ) | |
{ | |
# out of scope | |
} | |
elseif( $permission.Roles ) | |
{ | |
# out of scope | |
} | |
elseif( $permission.InheritedFrom ) | |
{ | |
# out of scope | |
} | |
} | |
} | |
} | |
} | |
end | |
{ | |
} | |
} | |
function ConvertTo-SiteId | |
{ | |
[CmdletBinding(DefaultParameterSetName="root")] | |
param | |
( | |
[Parameter(Mandatory=$true)] | |
[string] | |
$SiteUrl | |
) | |
begin | |
{ | |
} | |
process | |
{ | |
$uri = [System.Uri]$SiteUrl | |
"{0}:{1}" -f $uri.Host, $uri.AbsolutePath | |
} | |
end | |
{ | |
} | |
} | |
$null = Connect-MgGraph ` | |
-ClientId $env:O365_CLIENTID ` | |
-CertificateThumbprint $env:O365_THUMBPRINT ` | |
-TenantId $env:O365_TENANTID | |
$rows = Import-Csv -Path "C:\_temp\sites.csv" | |
$sharing = foreach( $row in $rows ) | |
{ | |
$siteUrl = $row.SiteUrl | |
$siteId = ConvertTo-SiteId -SiteUrl $siteUrl | |
$sharingLinks = Get-DriveItemSharingLink -SiteId $siteId -Verbose | |
$sharingLinks | |
} | |
$timestamp = Get-Date -Format FileDateTime | |
$sharing | Export-Csv -Path "C:\_temp\ExternalSharingLinks_$timestamp.csv" -NoTypeInformation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment