当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell 获取 SharePoint Online 文档库中所有文件的 URL

作者:精品下载站 日期:2024-12-14 15:00:26 浏览:14 分类:玩电脑

使用 PowerShell 获取 SharePoint Online 文档库中所有文件的 URL


要求:使用 PowerShell 获取 SharePoint Online 文档库中所有文件的 URL。

使用 PowerShell CSOM 获取 SharePoint Online 文档库中所有文件的 URL

您是否正在寻找 PowerShell 脚本来获取 SharePoint Online 中文件夹中所有文件的 URL?获取所有文件 URL 的列表对于各种目的(例如报告、迁移或审核)非常有用。在本文中,我们将了解如何使用 PowerShell 获取 SharePoint Online 文档库中所有文件的 URL。

好吧,下面是使用客户端对象模型获取特定文件夹中所有文件及其 URL 的列表的 PowerShell 脚本。


#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 Variables
$SiteURL= "https://crescent.sharepoint.com/sites/Marketing"
$ListName="Documents"
  
#Setup Credentials to connect
$Cred = Get-Credential
  
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

    #Get the Web
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $Ctx.ExecuteQuery()  

    #Get All Items from the List
    $List = $Ctx.web.Lists.GetByTitle($ListName)
    $ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()) 
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()
           
    $DataCollection = @()
    #Loop through each item 
    ForEach($ListItem in $ListItems)
    {
        #Calculate Absolute URL
        If($Web.ServerRelativeUrl -eq "/")
        {
            $AbsoluteURL=  $("{0}{1}" -f $Web.Url, $ListItem.FieldValues["FileRef"])
        }
        else
        {
            $AbsoluteURL=  $("{0}{1}" -f $Web.Url.Replace($Web.ServerRelativeUrl,''), $ListItem.FieldValues["FileRef"])
        } 

        #Collect data        
        $Data = New-Object PSObject -Property @{
            FileName  = $ListItem.FieldValues["FileLeafRef"]
            RelativeURL = $ListItem.FieldValues["FileRef"]
            AbsoluteURL = $AbsoluteURL
        }
        $DataCollection += $Data
    }
    $DataCollection | Format-List
}
Catch {
    write-host -f Red "Error: " $_.Exception.Message
}

PnP PowerShell 在 SharePoint Online 中获取文件 URL

如果您正在寻找一个 PowerShell 脚本来收集 SharePoint Online 中给定文件夹中所有文件的 URL,那就不用再犹豫了!让我向您展示如何使用 PnP PowerShell 快速获取文件夹中所有文件的 URL。


#Set Variables
$SiteURL= "https://crescent.sharepoint.com/sites/Marketing"
$ListName="Documents"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get All Items from the list - In batches of 500
$ListItems = Get-PnPListItem -List $ListName -PageSize 500

#Loop through List Items and Get File URL
$Results=@()
ForEach($Item in $ListItems)
{
    $Results += New-Object PSObject -Property @{
    FileName = $Item.FieldValues['FileLeafRef']
    FileURL = $Item.FieldValues['FileRef']
    }
}
$Results

这是另一篇文章:如何使用 PowerShell 从 SharePoint Online 中的文件夹获取所有文件?

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯