OneDrive PowerShell Module - New Version with Improved Authentication

Bild des Benutzers Marcel Meurer

A few months ago, I wrote a PowerShell module to access OneDrive. I got a lot of feedback from the community – thanks for that.

This week I published a newer version to PowerShellGallery.com including new features and bugfixes. These are the main improvements:

Bug fixes:

  • Filename and paths containing special characters like ‘=’, ‘$’, etc. are now handled correctly
  • Enumeration of folders with a file count >200 is now working 

Improvements:

  • Get-ODAuthentication now supports a “code” based-authentication including refresh of the authentication with a refresh token. This allows an unattended run of long running scripts.

How to log on to OneDrive using a code based authentication?

Hint: There is also a video on youtube showing this example: https://www.youtube.com/watch?v=yZNt0RT5wQs

Register your app/script for OneDrive

First, register your app/script at the Application Registration Portal:

  • Go to: https://apps.dev.microsoft.com and login with your Microsoft Account (MSA)
  • “Add an app” in the category “converged applications”:
  • Enter a name and press “create”
  • Press “Generate New Password“ and save the password (app key) 
  • Also save the “Application id”
  • Press “Add Platforms” and select “Web” 
  • 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  
  • Press “Save” 

Now you have all necessary data for your app / script:

  • Client Id: 5dd40b03-0ead-451b-b5e3-f704550e8cca
  • AppKey: xqacs8K92MuCJKgciRHQ1Cf
  • RedirectURI: http://localhost/login

Get an authentication token

To get an authentication token use:

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

 

Hint: Use “-AutoAccept” to avoid the confirmation windows

Test the authentication token

Save the authentication token to an object:

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

List files in a OneDrive-folder:

Get-ODChildItems -AccessToken $Auth.access_token -Path "/BACKUP Azure Global Bootcamp"

 

Renew the authentication token

An authentication token is valid for an hour. You can get a new “code” based token with the “refresh_token” from the login before:

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

 

Install the new version of OneDrive from PowerShellGallery.com

If you have the module PowerShellGet to access PowerShellGallery already installed, you can easily download this module with a single command (“-force” updates the module to the newest version):

Install-Module -Name OneDrive -Scope CurrentUser -force

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

If you have any questions or suggestions, write me an email or get in touch with me via www.twitter.com/MarcelMeurer 

Neuen Kommentar schreiben
Durch Absenden dieses Formulars akzeptieren Sie die Mollom Privatsphärenrichtlinie.