Download contents of Document library as PDF
Summary
Say we have lots of Office documents in a Document library. We need to have copies of the document stored on a local file share in case of network outages. We want the local copies to be in PDF format so people cant modify them.
This script will download the contents of a document library to a local directory , converting Office Documents to PDF in the process.
cls
# The client ID if an Azure AD app registration with graph read all sites app-only permission
# TODO: Enter value
$clientid=''
# The client Secret if an Azure AD app registration with graph read all sites app-only permission
# TODO: Enter value
$clientsecret=''
# The SharePoint tentant
# TODO: Enter value
$tenant="<tenant>.sharepoint.com"
# The azure tenant ID
# TODO: Enter value
$tenantid=""
# The Graph Drive ID found at https://tenant.sharepoint.com/sites/sitename/_api/v2.0/drives
# TODO: Enter value
$driveid=""
# The , three-part, Graph Site ID found at from https://tenant.sharepoint.com/sites/sitename/_api/v2.0/sites/root
# TODO: Enter value
$siteid="tenant.sharepoint.com,049287e5-abd9-472d-828b-a0a591ca2421,4d6b2467-70b2-46c6-a8a1-c8aa40f1bc9a" # from https://tenant.sharepoint.com/sites/sitename/_api/v2.0/sites/root
$filestoconvert='xlsx','docx','pptx'
$uri="https://login.microsoft.com/$tenantid/oauth2/v2.0/token"
$header = @{ ContentType = "application/x-www-form-urlencoded" }
$postbody = @{client_id=$clientid;scope='https://graph.microsoft.com/.default';client_secret=$clientsecret;grant_type='client_credentials'}
$authresponse = Invoke-RestMethod -Uri $uri -Headers $header -Body $postbody -Method Post
$token=$authresponse.access_token
$header = @{ Authorization = "Bearer $($token)" }
$uri = "https://graph.microsoft.com/v1.0/sites/$siteid/drives/$driveid/root/children"
do{
$response = Invoke-RestMethod -Uri $uri -Headers $header `
-Method Get -ContentType "application/json"
foreach ($item in $response.value){
Write-Host $item.name
if ($item.name.Contains('.')){
$fname=$item.name.Substring(0,$item.name.LastIndexOf("."))
$fext=$item.name.Substring($item.name.LastIndexOf(".")+1)
}else{
$fname=$item.name
$fext=''
}
if($filestoconvert.Contains($fext.ToLower())){
$uri = "https://graph.microsoft.com/v1.0/sites/$siteid/drives/$driveid/items/"+$item.id+"/content?format=pdf"
$outfile = "C:\temp\$fname.pdf"
} else
{
$uri = "https://graph.microsoft.com/v1.0/sites/$siteid/drives/$driveid/items/"+$item.id+"/content"
$outfile = "C:\temp\"+$item.name
}
Invoke-RestMethod -Uri $uri -Headers $header `
-Method Get -ContentType "application/json" -OutFile $outfile
}
$uri=$response.'@odata.nextLink'
}while ($uri -ne '')
# End
Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell
The way you login into PnP PowerShell has changed please read PnP Management Shell EntraID app is deleted : what should I do ?
Contributors
Author(s) |
---|
Russell Gove |
Adam Wójcik |
Disclaimer
THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.