The script exports data from a user OneDrive to a server folder. To run the script, you can use a custom command or scheduled task configured for the User object type.
For the script to work you will need to install a certificate for connection to SharePoint Online on the computer where Adaxes service runs. To do so:
- Create a certificate using the New-PnPAzureCertificate cmdlet.
- Assign the certificate to the Azure application whose credentials were used to register your Microsoft 365 tenant in Adaxes.
- Grant Azure application access to SharePoint (i.e. Sites.FullControl.All).
- Install the certificate on the computer where Adaxes service runs.
Parameters:
- $certificateThumbprint - Specifies the Thumbprint of the certificate that will be used to connect to SharePoint Online. For information on how to retrieve the Thumbprint, see How to: Retrieve the Thumbprint of a Certificate.
- $usernameMicrosoft365 - Specifies the LDAP name of the property that stores the value used to sign in to Microsoft 365 (Office 365).
- $destinationFolderPath - Specifies the path to the folder to which OneDrive data will be exported.
- $tenantName - Specifies the name of the Microsoft 365 tenant associated with the user. For information on how to check the tenant, see View Microsoft 365 tenant for a user.
- $oneDriveSiteURL - Specifies the full URL of the OneDrive site.
PowerShell
$certificateThumbprint = "9BCE7405DD63FD8DE7486FDD32D111667197BB8E" # TODO: modify me
$usernameMicrosoft365 = "%userPrincipalName%" # TODO: modify me
$destinationFolderPath = "\\Server\Share\%username%" # TODO: modify me
$tenantName = "MyTenant" # TOOD: modify me
$oneDriveSiteURL = "https://$tenantName-my.sharepoint.com/personal" # TODO: modify me
# Build OneDrive URL
$charsToReplace = @(".", "@")
$charsToReplace | %%{$usernameMicrosoft365 = $usernameMicrosoft365.Replace($_, "_")}
$oneDriveSiteURL = "$oneDriveSiteURL/$usernameMicrosoft365"
# Connecto to SharePoint Online
$tenant = $Context.CloudServices.GetO365Tenant()
$credential = $tenant.GetCredential()
try
{
$connection = Connect-PnPOnline -Url $oneDriveSiteURL -ClientId $credential.AppId -Thumbprint $certificateThumbprint -Tenant "$tenantName`.onmicrosoft.com" -ReturnConnection
# Get all items
try
{
$items = Get-PnPListItem -List Documents -ErrorAction Stop
}
catch
{
$Context.LogMessage("An error occurred when retrieving OneDrive items. Error: " + $_.Exception.Message, "Error")
return
}
if ($null -eq $items)
{
return # No items found
}
# Create directory structure
$folders = $items | Where-Object {$_.FileSystemObjectType -eq "Folder"}
$oneDrivePath = "/personal/$usernameMicrosoft365/Documents"
foreach ($folder in $folders)
{
$folderPath = $folder.FieldValues.FileRef.Replace($oneDrivePath, "").Replace("/", "\")
try
{
New-Item -Path "$destinationFolderPath$folderPath" -Force -ItemType "directory" -ErrorAction Stop
}
catch
{
$Context.LogMessage("An error occurred when creating the directory structure. Error: " + $_.Exception.Message, "Error")
return
}
}
# Download files
$files = $items | Where-Object {$_.FileSystemObjectType -eq "File"}
foreach ($file in $files)
{
$localFolderPath = $file.FieldValues.FileDirRef.Replace($oneDrivePath, "").Replace("/", "\")
try
{
Get-PnPFile -Url $file.FieldValues.FileRef -Path "$destinationFolderPath$localFolderPath" -AsFile -Filename $file.FieldValues.FileLeafRef -ErrorAction Stop
}
catch
{
$Context.LogMessage("An error occurred when downloading the file $($file.FieldValues.FileRef). Error: " + $_.Exception.Message, "Warning")
continue
}
}
}
finally
{
# Close the connection and release resources
if ($connection) { Disconnect-PnPOnline -Connection $connection }
}
Hello Derek,
Do we understand correctly that you are executing the script in Windows PowerShell? The thing is that the script uses the built-in Adaxes variable $Context and thus can only be executed in a Business Rule, Custom Command or Scheduled Task. Also, please, make sure that the user for which the script is executed has an associated Microsoft 365 (former Office 365) tenant registered in Adaxes. For details, have a look at step 8 of the following tutorial:https://www.adaxes.com/tutorials_ActiveDirectoryManagement_ManageAndAutomateOffice365.htm#collapse1.
This script works great. Thank you.
I have been struggling to modify it to change the destination to a SharePoint site.
Would you happen to have anything available? (I cant see anything by conducting a search)
Thank you
Jeff
Unfortunately, we do not have such examples. However, the following thread on Microsoft forums might be helpful:https://stackoverflow.com/questions/20237294/upload-file-to-sharepoint-document-library-using-powershell.
It works but the script will error out "The pipeline has been stopped." Is this due to the limitation of running a powershell script over approximately 9 minutes.
Thnx Remco
The error message means that the script execution exceeds the timeout configured in Adaxes. By default, the timeout is 10 minutes. As a solution, you can try running the script in a separate PowerShell process: https://www.adaxes.com/script-repository/run-script-in-new-powershell-instance-s290.htm.
# Connecto to SharePoint Online
$microsoft365Credential = $Context.GetOffice365Credential()
Connect-SPOService -Url $url -Credential $microsoft365Credential
With regards,
Remco Tiel
We are working on updating the script accordingly. Unfortunately, we are not yet aware if it is possible to realize the behavior with the new modules. Once there are any updates we will get back to you right away.
Thank you for your patience. We updated the script and its description accordingly. Please, check it above. For information on how to use an Azure application to register your Microsoft 365 tenant in Adaxes, have a look at the following help article: https://www.adaxes.com/help/RegisterAdaxesAsAppMicrosoftAzure.
"An error occurred when retrieving OneDrive items. Error: The attempted operation is prohibited because it exceeds the list view threshold."
It looks like the error occurs when the accessed lists include more than 5000 files. For information on how to remedy the issue, please, have a look at the following Microsoft article: https://support.microsoft.com/en-us/office/manage-large-lists-and-libraries-b8588dae-9387-48c2-9248-c24122f07c59?ui=en-us&rs=en-us&ad=us#ID0EABAAA=Server.