如何使用 PowerShell 从 SharePoint 获取所有文档的列表?

How to get a list of all documents from SharePoint using PowerShell?

我想使用 PowerShell 从 SharePoint 检索所有文档,但目前我只能检索特定站点的文档。

这是我使用的代码:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Set Parameters
$SiteURL="https://xxx.sharepoint.com/sites/CRMDevelopment"
$LibraryName="Documents"
$ReportOutput = "C:\users\xxx\downloads\VersionHistory.csv"

Try {
    #Setup Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials

    #Get the web & Library
    $Web=$Ctx.Web
    $Ctx.Load($Web)
    $List = $Web.Lists.GetByTitle($LibraryName)
    $Ctx.ExecuteQuery()

    #Get All Files of from the document library - Excluding Folders
    $Query =  New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml = "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq></Where><OrderBy><FieldRef Name='ID' /></OrderBy></Query></View>"
    $ListItems=$List.GetItems($Query)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()

    $VersionHistoryData = @()
    #Iterate throgh each version of file
    Foreach ($Item in $ListItems)
    {
        ##more code goes here.
    }
}

演示通过PnP PowerShell和SharePoint在线管理迭代所有网站集shell,如果要访问所有网站集中的库,该帐户需要访问所有网站集(admin没有权限)默认所有网站集)。

#region Variables 
$AdminName = "admin@xxx.onmicrosoft.com" 
$Password = "password"
#endregion Variables

#region Credentials 
[SecureString]$SecurePass = ConvertTo-SecureString $Password -AsPlainText -Force 
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $AdminName, $(convertto-securestring $Password -asplaintext -force)
#endregion Credentials
#Config Parameters
$AdminSiteURL="https://xxx-admin.sharepoint.com"
#$ReportOutput = "C:\users\xxx\downloads\VersionHistory.csv"

#Connect to SharePoint Online Admin Center
Connect-SPOService -Url $AdminSiteURL –Credential $credentials

#Get All site collections
$SiteCollections = Get-SPOSite -Limit All
Write-Host "Total Number of Site collections Found:"$SiteCollections.count -f Yellow

#Loop through each site collection and retrieve details
Foreach ($Site in $SiteCollections)
{
    #Test for specific site
#if($Site.URL -eq "https://xxx.sharepoint.com/sites/lee"){
    Write-Host "Processing Site Collection :"$Site.URL -f Yellow

    Connect-PnPOnline -Url $Site.URL -credentials $credentials
    $items=Get-PnPListItem -List Documents -Query "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq></Where><OrderBy><FieldRef Name='ID' /></OrderBy></Query></View>"
    Foreach ($Item in $items)
    {
        Write-Host $Item.FieldValues["FileRef"]
    }
#}
}
Write-Host "--"