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

[玩转系统] SharePoint Online:使用 PowerShell 从列表或库中获取所有文件夹

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

SharePoint Online:使用 PowerShell 从列表或库中获取所有文件夹


要求:使用 PowerShell 从 SharePoint Online 列表中获取所有文件夹。

[玩转系统] SharePoint Online:使用 PowerShell 从列表或库中获取所有文件夹

PowerShell 获取 SharePoint Online 列表中的所有文件夹

在许多情况下,您可能需要获取 SharePoint Online 列表或库中的所有文件夹的列表。这可以使用 PowerShell 来完成。这篇博文将向您展示如何使用 PowerShell 从列表或库中获取所有文件夹!

以下是基于 CAML 的方法,用于从列表中递归获取所有文件夹和子文件夹:


#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"

#Get 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 List
    $List = $Ctx.web.Lists.GetByTitle($ListName)
    
    #Define CAML Query to get all folders from list recursively
    $CAMLQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
    $CAMLQuery.ViewXml = "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FSObjType'/><Value Type='Integer'>1</Value></Eq></Where></Query></View>"

    #Get All Folders from the List
    $Folders = $List.GetItems($CAMLQuery)
    $Ctx.Load($Folders)
    $Ctx.ExecuteQuery()

    #Iterate through Each Folder
    ForEach($Folder in $Folders)
    {
        #Get the Folder's Server Relative URL
        Write-host $Folder.FieldValues['FileRef']
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

此 PowerShell 从 SharePoint Online 库获取文件夹列表。

SharePoint Online:使用 PowerShell 从 SharePoint Online 列表中获取子文件夹

让我们从 SharePoint Online 的列表或库中获取所有顶级文件夹。


#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"

#Get 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 List
    $List = $Ctx.web.Lists.GetByTitle($ListName)

    #Get Sub-Folders of the List
    $SubFolders = $List.RootFolder.Folders
    $Ctx.Load($SubFolders)
    $Ctx.ExecuteQuery()
    
    #Iterate through Each SubFolder
    ForEach($Folder in $SubFolders)
    {
        #Get the Folder's Server Relative URL
        Write-host $Folder.Name
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

同样,如果您想获取特定文件夹的所有子文件夹,请使用:


#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"
$FolderRelativeURL = "/sites/marketing/Shared Documents/2018"

#Get 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 Sub-Folders of a Folder
    $SubFolders = $Ctx.web.GetFolderByServerRelativeUrl($FolderRelativeURL).Folders
    $Ctx.Load($SubFolders)
    $Ctx.ExecuteQuery()
    
    #Iterate through Each SubFolder
    ForEach($Folder in $SubFolders)
    {
        #Get the Folder's Server Relative URL
        Write-host $Folder.Name
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

SharePoint Online:使用 PowerShell 从 SharePoint Online 列表中递归获取所有文件夹

以下是用于从 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"

#Function to Get Sub-folders of a Folder in SharePoint Online
Function Get-SPOFolders([Microsoft.SharePoint.Client.Folder]$Folder)
{
    Try {         
        Write-host $Folder.ServerRelativeUrl
 
        #Process all Sub Folders
        $Ctx.Load($Folder.Folders)
        $Ctx.ExecuteQuery()
 
        #Iterate through each sub-folder of the folder
        Foreach ($Folder in $Folder.Folders)
        {
            #Call the function recursively
            Get-SPOFolders $Folder
        }
    }
    Catch {
        write-host -f Red "Error Getting Folder!" $_.Exception.Message
    }
}

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

#Get 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 Root Folder of a List
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $RootFolder = $List.RootFolder
    $Ctx.Load($RootFolder)
    $Ctx.ExecuteQuery()
    
    #Call the Function to get all folders recursively from the list
    Get-SPOFolders $RootFolder
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

PnP PowerShell 从 SharePoint Online 文档库获取所有文件夹

以下是如何从 SharePoint Online 列表或库中递归获取所有文件夹(和子文件夹):


#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Branding"
  
Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
  
    #Get All Folders from the document Library
    $Folders = Get-PnPFolder -List $ListName
     
    Write-host "Total Number of Items in the Folder in the list:" $Folders.Count

    #Get Folder/Subfolder details
    $Folders | Select Name, TimeCreated, ItemCount, ServerRelativeUrl
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

让我们从给定文档库中获取所有文件夹,并使用 PnP PowerShell 将它们导出到 CSV 文件。


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/legal"
$ListName = "Work"
$CSVPath = "C:\Temp\Folders.csv"
$FoldersInventory = @()

#Connect to Site
Connect-PnPOnline -Url $SiteURL -Interactive

#Get the List
$List =  Get-PnPList -Identity $ListName

#Get all Folders from List - with progress bar
$global:counter = 0;
$Folders = Get-PnPListItem -List $List -PageSize 500 -Fields FileLeafRef -ScriptBlock { Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete ($global:Counter / ($List.ItemCount) * 100) -Activity "Getting Folders from List:" -Status "Processing Items $global:Counter to $($List.ItemCount)";}  | Where {$_.FileSystemObjectType -eq "Folder"}
  
#Iterate through all folders in the list
$Folders | ForEach-Object {
    #Collect Folder data
    $FoldersInventory += [PSCustomObject] @{
        FolderName = $_.FieldValues.FileLeafRef
        URL = $_.FieldValues.FileRef
    }   
}
$FoldersInventory
$FoldersInventory | Export-Csv -Path $CSVPath -NoTypeInformation

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

取消回复欢迎 发表评论:

关灯