2016-02-05 48 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

相關問題