Constructing OneDrive URL

Introduction

 
Recently, I had a requirement to construct the OneDrive URL from the email address. As many of us know, OneDrive is a cloud storage service provided by Microsoft, which lets the user store up to 25 TB. Users are required to have a proper subscription (E1, E3, E5, etc) to utilize this service. 
 
A couple of points to note, if you observe the URL of the One Drive for an organization, let's consider Contoso. If the email of the user is vinay.deep@contoso.com, (usually the email address will be in the format of firstname.lastname@organizationname.com). Then the one drive URL would be:
 
https://contoso-my.sharepoint.com/personal/vinay_deep_contoso_com/_layouts/15/OneDrive.aspx  
 
Let's try understanding by breaking down the URL,
  • https://contoso-my.sharepoint.com - This will be in format https://[YOURORGNAME]-my.sharepoint.com 
  • /personal - it is the managed path defined for all OneDrive sites
  • /vinay_deep_contoso.com - observe the email address is vinay.deep@contoso.com. It replaces special characters with '_'.
  • /_layouts/15/onedrive.aspx - it is constant to access one drive pages

Steps

 
To get the final OneDrive URL from the given email address, I have followed the below steps. At a glance,
  • Getting the user input from the command prompt,
  • Extracting the org name from the email.
  • Building the URL string from email, basically replacing special characters with underscore '_'.
  • Constructing the URL.
I have used Powershell with a regular expression to achieve the result set.
 
Step 1
 
Getting the user input from the command prompt. Just read the user input from the command prompt and storing in $Email variable,
  1. $Email = Read-host "Enter the Email ID of user"   
Step 2
 
Extracing the org name from the given email address. Here I am storing the regex pattern in variable called '$pattern' and then finally matching with given email to extract the string which is organization name here,
  1. $pattern = '(?<=\@).+?(?=\.)'  
  2. $OrgName = [regex]::Matches($Email, $pattern).Value  
Step 3
 
Building the One Drive string from the email. To replace the special character, I have written the function here called 'Construct-OneDriveString' from the given email.
  1. function Construct - OneDriveString {  
  2.     param(  
  3.         [string] $InputString,  
  4.         [string] $Replacement = "_",  
  5.         [string] $SpecialChars = ".@")  
  6.     $rePattern = ($SpecialChars.ToCharArray() | ForEach - Object {  
  7.         [regex]::Escape($_)  
  8.     }) - join "|"  
  9.     $InputString - replace $rePattern, $Replacement  
  10. }   
Step 4
 
Finally, constructing the OneDrive URL from the values generated from above steps. We have got the OneDrive string and organization name, which is enough to build the URL.  
  1. $OneDriveUrl = "https://$OrgName-my.sharepoint.com/personal/" + $OutputString + "/_layouts/15/onedrive.aspx"  
Complete Script
  1. function Construct - OneDriveString {  
  2.     param(  
  3.         [string] $InputString,  
  4.         [string] $Replacement = "_",  
  5.         [string] $SpecialChars = ".@")  
  6.     $rePattern = ($SpecialChars.ToCharArray() | ForEach - Object {  
  7.         [regex]::Escape($_)  
  8.     }) - join "|"  
  9.     $InputString - replace $rePattern, $Replacement  
  10. }  
  11. $Email = Read - host "Enter the Email ID of user"  
  12. $OutputString = Construct - OneDriveString $Email  
  13. $pattern = '(?<=\@).+?(?=\.)'  
  14. $OrgName = [regex]::Matches($Email, $pattern).Value  
  15. $OneDriveUrl = "https://$OrgName-my.sharepoint.com/personal/" + $OutputString + "/_layouts/15/onedrive.aspx"  
  16. #Checking the URL  
  17. try {  
  18.     Write - Host "verifying the url $OneDriveUrl" - ForegroundColor Yellow  
  19.     $CheckConnection = Invoke - WebRequest - Uri $OneDriveUrl  
  20.     if ($CheckConnection.StatusCode - eq 200) {  
  21.         Write - Host "Connection Verified" - ForegroundColor Green  
  22.     }  
  23. catch [System.Net.WebException] {  
  24.     $ExceptionMessage = $Error[0].Exception  
  25.     if ($ExceptionMessage - match "403") {  
  26.         Write - Host "URL exists, but you are not authorized" - ForegroundColor Yellow  
  27.     }  
  28.     elseif($ExceptionMessage - match "503") {  
  29.         Write - Host "Server Busy" - ForegroundColor Red  
  30.     }  
  31.     elseif($ExceptionMessage - match "404") {  
  32.         Write - Host "URL doesn't exists" - ForegroundColor Red  
  33.     }  
  34.     else {  
  35.         Write - Host "There is an error" - ForegroundColor Red  
  36.     }  
  37. }   
In this script, after completing the construction of the OneDrive URL, I am also checking the connection to One Drive by using Invoke-WebRequest function, which is built in a function used to invoke a web request to any https/http URLs and have the response back. 
 
If the resultant OneDrive URL is valid, the user will be given a success message called 'connection verified'. 
 
Constructing OneDrive URL
 
If the resultant OneDrive URL is not valid, the user will be given a failure message called 'URL doesn't exist.
 
Constructing OneDrive URL
 

Conclusion

 
Thus, in this article, we have seen how to construct the OneDrive URL from the user's email input. Hope you find this article useful. 
 
References
 
  • https://powershellone.wordpress.com/2021/02/24/using-powershell-and-regex-to-extract-text-between-delimiters 
  • https://stackoverflow.com/questions/20259251/powershell-script-to-check-the-status-of-a-url