Marcel
Marcel That's me: Marcel

OneDrive PowerShell Module - Added support for OneDrive for Business

OneDrive PowerShell Module - Added support for OneDrive for Business

More than two years ago, I created my PowerShell module to access OneDrive. This module can be installed with a one-liner from https://www.powershellgallery.com/packages/OneDrive

Again, I was asked to support OneDrive for Business and finally, I’m ready: From version 2.0.0 OneDrive for Business is supported.

I provide the complete documentation on GitHub, where I will maintain it: https://github.com/MarcelMeurer/PowerShellGallery-OneDrive

Here is the summary of version 2.0.0:

----------------------------------------------

The OneDrive PowerShell module is available via PowerShellGallery.com. If you want to support and work with me feel free to make changes cloning this repo, change and send me and a pull request.

This OneDrive version (2.0.0 and higher in PowerShellGallery.com) supports:

  • OneDrive personal
  • OneDrive for Business

Installation

Open PowerShell and

Install-Module -Name OneDrive -Scope CurrentUser -force

You can update the module to a newer version with the same command (-force). If you don’t use PowerShellGet currently, go to the Gallery on https://www.powershellgallery.com/packages/OneDrive and click "Get Started".

Check your installation with

Get-Help -Name OneDrive

Authentication

Before you start using the OneDrive module you have registered your script/application. This differs depending on the OneDrive version to be used.

OneDrive Personal

Read this on my blog: https://www.sepago.de/blog/onedrive-powershell-module-new-version-with-improved-authentication/ - Go to: https://apps.dev.microsoft.com and login with your Microsoft Account (MSA) and "Add an app" in the category "converged applications" img - Enter a name and press "create" img - Press "Generate New Password" and save the password (app key) img - Also, save the "Application id" - Press "Add Platforms" and select "Web" img - Check "Allow implicit Flow" and enter a "Redirect URL". This is not a real URL. Choose a localhost address and note it. In my case, I chose: http://localhost/login img img - Press "Save" - Now you have all the necessary data for your app/script: - Client Id: 5dd40b03-0ead-451b-b5e3-f704550e8cca - AppKey: xqacs8K92MuCJKgciRHQ1Cf - RedirectURI: http://localhost/login - To get an authentication token use:

$Auth=Get-ODAuthentication -ClientID 5dd40b03-0ead-451b-b5e3-f704550e8cca -AppKey xqacs8K92MuCJKgciRHQ1Cf -RedirectURI http://localhost/login

img

OneDrive for Business

To use OneDrive for business you have to register your script/app to in Azure Active Directory

  • Add an application in Azure Active Directory inside the Azure portal: https://portal.azure.com/#blade/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/RegisteredApps
  • Chose "New application registration"
  • Give your application a name and a unique sign-on URL. The sign-on URL has to be a valid URL but doesn't have to exist. E.g.: http://sepago.de/1Drive4Business (make later sure that this URL is in the reply URL list of your application) img

  • Within the "Required permissions" add "Office 365 SharePoint Online (Microsoft.Sharepoint)" img

  • Select "Read and write user files" below "delegated permissions" for the Office 365 API img

  • Generate a secret key for this application and save it for later use. Also, save the application Id img img

  • You should now have the following parameter:

  • Additionally you need the resource URL for OneDrive for Business. Normally: https://-my.sharepoint.com/. In our company this is the URL "

https://sepagogmbh-my.sharepoint.com/

" (the last one / is important).

$Auth=Get-ODAuthentication -ClientId "2831fc52-e1b8-4493-9f3a-a3dad74b2081" -AppKey "TqoSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX="  -RedirectURI "http://sepago.de/1Drive4Business" -ResourceId "https://sepagogmbh-my.sharepoint.com/"

Renew the authentication with a refresh token

An access token is 1 hour valid. You can get a new access token with the refresh token provided by the last authentication. This is necessary if you are creating a script that will work for a long time without further user input. Renew your access token automatically in the program code.

$Auth=Get-ODAuthentication -ClientId 2831fc52-e1b8-4493-9f3a-a3dad74b2081 -AppKey "TqoSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX=" -RedirectURI "http://sepago.de/1Drive4Business" -ResourceId "https://sepagogmbh-my.sharepoint.com/" -RefreshToken $LastAuth.refresh_token
  • Where $LastAuth is your last authentication result (containing the refresh token)
  • For OneDrive personal leave the ResourceId empty (-ResourceId "")

Working with files and folders

Get an authentication code from above and store it in $Auth

List files and folders

Get-ODChildItems -AccessToken $Auth.access_token -ResourceId "https://sepagogmbh-my.sharepoint.com/" -path "/"

List files and folders

Remove-ODItem -AccessToken $Auth.access_token -ResourceId "https://sepagogmbh-my.sharepoint.com/" -path "/Upload"

Creating a folder

New-ODFolder -AccessToken $Auth.access_token -ResourceId "https://sepagogmbh-my.sharepoint.com/" -path "/" -FolderName "Upload"

Upload local files to OneDrive

Add-ODItem -AccessToken $Auth.access_token -ResourceId "https://sepagogmbh-my.sharepoint.com/" -LocalFile "D:\DEV\PowerShell\PowerShellGallery-OneDrive\Test\Uploads\IoT Workshop.pptx" -Path "/Upload"

List OneDrive drives

Get-ODDrives -AccessToken $Auth.access_token -ResourceId "https://sepagogmbh-my.sharepoint.com/"

Downloading some files

Get-ODItem -AccessToken $Auth.access_token -ResourceId "https://sepagogmbh-my.sharepoint.com/" -Path "/Upload/Doings.txt" -LocalPath "D:\DEV\PowerShell\PowerShellGallery-OneDrive\Test\Downloads"

Get-ODItem -AccessToken $Auth.access_token -ResourceId "https://sepagogmbh-my.sharepoint.com/" -Path "/Upload/Doings.txt" -LocalPath "D:\DEV\PowerShell\PowerShellGallery-OneDrive\Test\Downloads" -LocalFileName "Copy from OneDrive.Doings.txt"

Delete a file in OneDrive

Remove-ODItem -AccessToken $Auth.access_token -ResourceId "https://sepagogmbh-my.sharepoint.com/" -Path "/Upload/Doings.txt"

First published on: https://www.sepago.de/blog/onedrive-powershell-module-added-support-for-onedrive-for-business/