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

[玩转系统] SharePoint Online:使用 PowerShell 获取文档库

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

SharePoint Online:使用 PowerShell 获取文档库


要求:使用 PowerShell 在 SharePoint Online 中获取文档库。

[玩转系统] SharePoint Online:使用 PowerShell 获取文档库

SharePoint Online:使用 PowerShell 获取文档库

使用 PowerShell 和 SharePoint Online 时,有时您可能需要获取文档库。有多种方法可以通过 Powershell 与 SharePoint Online 进行交互。这篇博文将介绍如何使用 PowerShell 获取文档库。这对于自动化任务或报告目的非常有用。让我们开始吧!

以下是获取 SharePoint Online 文档库的 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"

#Variables
$SiteURL = "https://crescent.sharepoint.com/Sites/Marketing"
$DocLibraryName="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)
 
    #sharepoint online get a document library powershell
    $DocLibrary = $Ctx.Web.Lists.GetByTitle($DocLibraryName)
    $Ctx.Load($DocLibrary)
    $Ctx.ExecuteQuery()

    Write-host "Total Number of Items in the Document Library:"$DocLibrary.ItemCount
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

用于在 SharePoint Online 中获取文档库的 PnP PowerShell:

要使用 PowerShell 获取文档库,您首先需要使用 Connect-PnpOnline cmdlet 连接到 SharePoint Online 网站。连接后,您可以使用 Get-PnPList cmdlet 获取文档库。您需要指定一些参数,例如库的名称和站点 URL,PowerShell 将处理其余的事情。

要获取 SharePoint Online 站点中的文档库,请使用以下 PnP PowerShell 脚本:


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$LibraryName = "Documents"

#Connect to the Site
Connect-PnPOnline -URL $SiteURL -Interactive

#sharepoint online powershell get document library
$DocumentLibrary = Get-PnPList -Identity $LibraryName

#Get Number of Items the Document Library
Write-host $DocumentLibrary.ItemCount

此脚本获取文档库并返回库中的项目总数。

结论:

总之,使用 PowerShell 在 SharePoint Online 网站中获取文档库是一个简单的过程。通过连接到 SharePoint Online 网站,您可以获得特定文档库或网站上所有文档库的列表。这对于管理或处理大量内容非常有用。可以轻松修改本教程中提供的示例脚本以满足您的特定需求。

如何使用 PowerShell 获取 SharePoint Online 中的文档库列表?使用:SharePoint Online:PowerShell 获取所有文档库

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

取消回复欢迎 发表评论:

关灯