2016-02-05 42 views
2

我对PowerShell很新,想知道是否有办法从OneDrive中提取所有文件并查看谁有权访问它们?Powershell脚本查看所有OneDrive文件和谁有权访问

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

截至目前,我知道如果你通过每个用户帐户,你可以看到这个信息。我很想知道是否有更快的方法。

回答

1

您可以致电OneDrive List Shared File Rest API完成此项工作。

您需要注册一个应用程序到你OneDrive适当的访问权限根据https://dev.onedrive.com/app-registration.htm

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

$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

相关问题