Skip to content

Instantly share code, notes, and snippets.

@joerodgers
Created November 9, 2020 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joerodgers/b9604958fbf7d56988973c1efd7f17f2 to your computer and use it in GitHub Desktop.
Save joerodgers/b9604958fbf7d56988973c1efd7f17f2 to your computer and use it in GitHub Desktop.
Returns the OneDrive Site Url by looking up the user in Microsoft Graph by either email address or UPN.
[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
[System.Net.ServicePointManager]::SecurityProtocol = "Tls11", "Tls12"
Import-Module SharePointPnPPowerShellOnline
function Get-DriveUrl
{
[cmdletbinding(DefaultParameterSetName='UserPrincipalName')]
param
(
[Parameter(Mandatory=$true,ParameterSetName='UserPrincipalName')]
[string]$UserPrincipalName,
[Parameter(Mandatory=$true,ParameterSetName='EmailAddress')]
[string]$EmailAddress
)
begin
{
$token = Get-PnPGraphAccessToken
}
process
{
if( $PSCmdlet.ParameterSetName -eq "EmailAddress" )
{
$uri = "https://graph.microsoft.com/v1.0/users?`$filter=mail eq '$EmailAddress'"
$user = Invoke-RestMethod -Method Get -Uri $uri -Headers @{ accept = "application/json"; Authorization = "Bearer $token"} | SELECT -ExpandProperty Value
$UserPrincipalName = $user.userPrincipalName
}
Invoke-RestMethod -Method Get -Uri "https://graph.microsoft.com/v1.0/users/$UserPrincipalName/drive" -Headers @{ accept = "application/json"; Authorization = "Bearer $token"} | SELECT -ExpandProperty WebUrl | % { $_ -replace "/Documents$", ""}
}
end
{
}
}
$tenant = "contoso"
$clientId = "8a6b10a8-1234-1234-1234-9b8e49b6f6b7"
$CertificatePath = "C:\_powershell\AADAppPrincipalCertificates\AppPrincipalCert.pfx"
$CertificatePassword = ConvertTo-SecureString -String "pass@word1" -AsPlainText -Force
Get-DriveUrl -UserPrincipalName "adamb@josrod.com"
Get-DriveUrl -EmailAddress "adam.bar@contoso.com"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment