112 lines
3.3 KiB
PowerShell
Executable File
112 lines
3.3 KiB
PowerShell
Executable File
$SiteUrl = "https://zefiromethane.sharepoint.com/sites/contentTypeHub"
|
|
$Destination = "F:\TeamSite"
|
|
|
|
# Opens a Microsoft sign-in window.
|
|
Connect-MgGraph -Scopes "Sites.Read.All" -NoWelcome
|
|
|
|
function Get-GraphCollection {
|
|
param([string]$Uri)
|
|
|
|
do {
|
|
$response = Invoke-MgGraphRequest -Method GET -Uri $Uri
|
|
|
|
foreach ($item in @($response.value)) {
|
|
Write-Output $item
|
|
}
|
|
|
|
$Uri = $response.'@odata.nextLink'
|
|
}
|
|
while ($Uri)
|
|
}
|
|
|
|
function Copy-GraphFolder {
|
|
param(
|
|
[string]$DriveId,
|
|
[string]$ChildrenUri,
|
|
[string]$LocalFolder
|
|
)
|
|
|
|
New-Item -ItemType Directory -Path $LocalFolder -Force |
|
|
Out-Null
|
|
|
|
foreach ($item in Get-GraphCollection -Uri $ChildrenUri) {
|
|
$localPath = Join-Path $LocalFolder $item.name
|
|
$escapedDriveId = [Uri]::EscapeDataString($DriveId)
|
|
$escapedItemId = [Uri]::EscapeDataString([string]$item.id)
|
|
|
|
if ($null -ne $item.folder -or $null -ne $item.package) {
|
|
# Create the folder, including empty folders.
|
|
New-Item -ItemType Directory -Path $localPath -Force |
|
|
Out-Null
|
|
|
|
$childUri = `
|
|
"https://graph.microsoft.com/v1.0/drives/" +
|
|
"$escapedDriveId/items/$escapedItemId/children"
|
|
|
|
Copy-GraphFolder `
|
|
-DriveId $DriveId `
|
|
-ChildrenUri $childUri `
|
|
-LocalFolder $localPath
|
|
}
|
|
elseif ($null -ne $item.file) {
|
|
if (Test-Path -LiteralPath $localPath) {
|
|
Write-Host "Skipping existing file: $localPath"
|
|
continue
|
|
}
|
|
|
|
Write-Host "Downloading: $localPath"
|
|
|
|
$contentUri = `
|
|
"https://graph.microsoft.com/v1.0/drives/" +
|
|
"$escapedDriveId/items/$escapedItemId/content"
|
|
|
|
Invoke-MgGraphRequest `
|
|
-Method GET `
|
|
-Uri $contentUri `
|
|
-OutputFilePath $localPath
|
|
}
|
|
else {
|
|
Write-Warning "Unsupported item skipped: $($item.name)"
|
|
}
|
|
}
|
|
}
|
|
|
|
# Resolve the SharePoint site from its URL.
|
|
$parsedSiteUrl = [Uri]$SiteUrl
|
|
$sitePath = $parsedSiteUrl.AbsolutePath.TrimEnd("/")
|
|
|
|
if ([string]::IsNullOrWhiteSpace($sitePath)) {
|
|
$siteLookupUri = `
|
|
"https://graph.microsoft.com/v1.0/sites/$($parsedSiteUrl.Host)"
|
|
}
|
|
else {
|
|
$siteLookupUri = `
|
|
"https://graph.microsoft.com/v1.0/sites/" +
|
|
"$($parsedSiteUrl.Host):$sitePath"
|
|
}
|
|
|
|
$site = Invoke-MgGraphRequest -Method GET -Uri $siteLookupUri
|
|
|
|
New-Item -ItemType Directory -Path $Destination -Force |
|
|
Out-Null
|
|
|
|
# A Graph "drive" represents a SharePoint document library.
|
|
$drivesUri = "https://graph.microsoft.com/v1.0/sites/$($site.id)/drives"
|
|
|
|
foreach ($drive in Get-GraphCollection -Uri $drivesUri) {
|
|
$localLibrary = Join-Path $Destination $drive.name
|
|
$escapedDriveId = [Uri]::EscapeDataString([string]$drive.id)
|
|
|
|
Write-Host "Downloading library: $($drive.name)"
|
|
|
|
$rootChildrenUri = `
|
|
"https://graph.microsoft.com/v1.0/drives/" +
|
|
"$escapedDriveId/root/children"
|
|
|
|
Copy-GraphFolder `
|
|
-DriveId $drive.id `
|
|
-ChildrenUri $rootChildrenUri `
|
|
-LocalFolder $localLibrary
|
|
}
|
|
|
|
Disconnect-MgGraph |