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 [email protected], (usually the email address will be in the format of [email protected]). 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 [email protected]. 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,
$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,
$pattern = '(?<=\@).+?(?=\.)'  
$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.
function Construct - OneDriveString {  
    param(  
        [string] $InputString,  
        [string] $Replacement = "_",  
        [string] $SpecialChars = ".@")  
    $rePattern = ($SpecialChars.ToCharArray() | ForEach - Object {  
        [regex]::Escape($_)  
    }) - join "|"  
    $InputString - replace $rePattern, $Replacement  
}   

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.  

$OneDriveUrl = "https://$OrgName-my.sharepoint.com/personal/" + $OutputString + "/_layouts/15/onedrive.aspx"  

Complete Script

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
 
Complete Script: 
function Construct-OneDriveString {
  <#
    .Description
    This function takes the email input as string and replaces the special characters with '_' to construct OneDrive URL string
  #>
    param(
        [string]$InputString,
        [string]$Replacement  = "_",
        [string]$SpecialChars = ".@"
    )

    $rePattern = ($SpecialChars.ToCharArray() |ForEach-Object { [regex]::Escape($_) }) -join "|"

   $InputString -replace $rePattern,$Replacement
    
} 
$Email = Read-host "Enter the Email ID of user"  
$OutputString = Construct-OneDriveString $Email  
$pattern = '(?<=\@).+?(?=\.)'  
$OrgName = [regex]::Matches($Email, $pattern).Value  
$OneDriveUrl = "https://$OrgName-my.sharepoint.com/personal/" + $OutputString + "/_layouts/15/onedrive.aspx"  
#Checking the URL  
try { 
    Write-Host "verifying the url $OneDriveUrl" - ForegroundColor Yellow  
    $CheckConnection = Invoke-WebRequest -Uri $OneDriveUrl  
    if ($CheckConnection.StatusCode -eq 200) {  
        Write-Host "Connection Verified" -ForegroundColor Green  
    }  
} catch [System.Net.WebException] {  
    $ExceptionMessage = $Error[0].Exception  
    if ($ExceptionMessage -match "403") {  
        Write-Host "URL exists, but you are not authorized" -ForegroundColor Yellow  
    }  
    elseif($ExceptionMessage -match "503") {  
        Write-Host "Server Busy" -ForegroundColor Red  
    }  
    elseif($ExceptionMessage -match "404") {  
        Write-Host "URL doesn't exists" -ForegroundColor Red  
    }  
    else {  
        Write-Host "There is an error" - ForegroundColor Red  
    }  
}

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


Similar Articles