Reputation: 145
I'm pretty new to PowerShell and wanted to know if there was a way to pull all files from OneDrive and see who has access to them?
I was hoping to find an easier way to see whether or not a file is shared and if it is, who is it shared with internally and externally.
As of right now, I know if you go through each user account, you can see this information. I'm curious to know if there is a faster way to that.
Upvotes: 2
Views: 11432
Reputation: 1118
You can call OneDrive List Shared File Rest API to finish the job.
You need register an application to get proper access to your OneDrive according to https://dev.onedrive.com/app-registration.htm
Then you can make use the code below.
$ClientId = "<Your application client id>" # your application clientid
$SecrectKey = "<Your application key>" # the secrect key for your application
$RedirectURI = "<Your web app redirect url>" # the re-direct url of your application
Function List-SharedItem
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true)][String]$ClientId,
[Parameter(Mandatory=$true)][String]$SecrectKey,
[Parameter(Mandatory=$true)][String]$RedirectURI
)
# import the utils module
Import-Module ".\OneDriveAuthentication.psm1"
# get token
$Token = New-AccessTokenAndRefreshToken -ClientId $ClientId -RedirectURI $RedirectURI -SecrectKey $SecrectKey
# you can store the token somewhere for the later usage, however the token will expired
# if the token is expired, please call Update-AccessTokenAndRefreshToken to update token
# e.g.
# $RefreshedToken = Update-AccessTokenAndRefreshToken -ClientId $ClientId -RedirectURI $RedirectURI -RefreshToken $Token.RefreshToken -SecrectKey $SecrectKey
# construct authentication header
$Header = Get-AuthenticateHeader -AccessToken $Token.AccessToken
# api root
$ApiRootUrl = "https://api.onedrive.com/v1.0"
# call api
$Response = Invoke-RestMethod -Headers $Header -Method GET -Uri "$ApiRootUrl/drive/shared"
RETURN $Response.value
}
# call method to do job
$Results = List-SharedItem -ClientId $ClientId -SecrectKey $SecrectKey -RedirectURI $RedirectURI
# print results
$Results | ForEach-Object {
Write-Host "ID: $($_.id)"
Write-Host "Name: $($_.name)"
Write-Host "ParentReference: $($_.parentReference)"
Write-Host "Size: $($_.size)"
Write-Host "WebURL: $($_.webUrl)"
Write-Host
}
For complete instructions, you can see the sample in https://gallery.technet.microsoft.com/How-to-use-OneDrive-Rest-5b31cf78
Upvotes: 2