Posted in : Azure, Microsoft, Office 365, Powershell, Security Av Stina Perbo Utas Översätt med Google ⟶

4 years ago

OneDrive for Business is great! The administration tools provided is not!
If you’re still reading you might agree with me when I say that Sharepoint Online admin center is far from the best experience when administrating anything related to OneDrive. Luckily we have access to everything we might need using the OneDrive REST API, a portion of the beloved Microsoft Graph API.
Whether migrating to Onedrive, applying new guidelines/compliance rulesor simply auditing the Onedrive permissions you will want to do it using Microsoft Graph. The functions mentioned here is available in the end of the post as well as a post with further reading about how to use the function.
So today we are going to have a detailed look on how to audit OneDrive sharing permission as well as cleaning up any unwanted configurations.
As we are using an Azure AD Application to audit and clean we are going to need the following Application permissions are required to inspect and remove unwanted sharing permissions.

With our Application configured we can query for a users files. However the API only lists files per folder and has at the time of this writing no recursive option. Thus we need to perform the enumeration recursively ourselves in the form of a function called ”Get-OneDriveItems”.

Function Get-OneDriveItems {
    $AllItems = @()
        $AllItems += Invoke-MSGraphQuery -URI "$UserId/drive/items/$itemId/children" -token $token -tokenrefresh -recursive -credential $credential -tenantID $TenantID | Select -ExpandProperty value
        $AllItems += Invoke-MSGraphQuery -URI "$UserId/drive/root/children" -token $token -tokenrefresh -recursive -credential $credential -tenantID $TenantID | Select -ExpandProperty value
    $AllItems | Where-Object{$_.folder.childCount -ge 1} | ForEach-Object{
        $AllItems += Get-OneDriveItems -UserId $UserId -token $token -credential $credential -TenantID $TenantID -itemId $_.Id
    Write-Verbose "Found $($AllItems.Count) files in folder $itemId"
    Return $AllItems

To list all files we run the function against a AzureAD UserID. We simply select a user and run the following line of code.

$AllItems = Get-OneDriveItems -UserId $ -token $token -credential $credential -TenantID $TenantID

Now when we have all the files we need to do a permission get request per item. Given that default storage size is up to one terabyte, this can take a LONG time and probably needs to be optimized using json batching and/or threading, examples are found in my other post at the end of the post. For simplicity I’m just going to demonstrate how to do this in a sequential fashion.

$AllPermissions = $AllItems | ForEach-Object{
    $uri = "$($$($"
    Invoke-MSGraphQuery -URI $uri -token $token -tokenrefresh -credential $credential -tenantID $TenantID -method get

Now when we have the permissions for all files, we can inspect them. It’s important to undestand how a OneDrive permission object is constructed in order to later filter for them. Check out the documentation for more information.

In the example below, we’re going to DELETE all anonymous sharing permissions, and we do this by listing all permissions on line 4.

$AllPermissions | ForEach-Object{
    $thisPermission = $_
    $fileId = $thisPermission.'@odata.context'.Split("'")[-2]
    [array]$permissionsToRemove = $thisPermission.value | Where-Object{$ -eq 'anonymous'}
    $permissionsToRemove | ForEach-Object{
        Invoke-MSGraphQuery -URI "$($$fileId/permissions/$($" -token $token -tokenrefresh -credential $credential -tenantID $TenantID -method DELETE

If you want to add filters, for example remove all non-external permissions we can add the filter below as an -OR

$ -notlike "*"

And there we have it. The organization files are no longer in risk of external exposure!

If you’re interested in Microsoft GraphAPI don’t hesitate to send me an email at
Thanks for reading!

Github project:
More Microsoft GraphAPI:
OneDrive/OneDrive for Business/Sharepoint Document Libraries REST API:
Manage Sharing using SharePoint Online Admin Center:

Tags : Azure, Azure AD, Microsoft, OD4B, ODFB, Onedrive, OneDrive for business, PowerShell, Security, Sharing, Windows

Personlig rådgivning

Vi erbjuder personlig rådgivning med författaren för 1400 SEK per timme. Anmäl ditt intresse i här så återkommer vi så snart vi kan.

Add comment

Your comment will be revised by the site if needed.