Powershell脚本,用于查看所有OneDrive文件以及谁有权访问它们

时间:2016-02-05 20:07:19

标签: powershell office365 onedrive

我是PowerShell的新手,想知道是否有办法从OneDrive中提取所有文件并查看谁可以访问它们?

我希望找到一种更简单的方法来查看文件是否共享,如果是,是谁在内部和外部共享。

截至目前,我知道如果您浏览每个用户帐户,就可以看到此信息。我很想知道是否有更快的方法。

1 个答案:

答案 0 :(得分:1)

您可以拨打OneDrive List Shared File Rest API来完成这项工作。

您需要注册一个应用程序,以便根据https://dev.onedrive.com/app-registration.htm

正确访问您的OneDrive

然后你可以使用下面的代码。

$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 
}

有关完整说明,您可以在https://gallery.technet.microsoft.com/How-to-use-OneDrive-Rest-5b31cf78

中查看示例