powershell 仅获取文档库内的第一级文件夹(主文件夹),似乎“Get-PnPFolder -List $Library”检索所有子文件夹

crcmnpdw  于 2023-03-02  发布在  Shell
关注(0)|答案(1)|浏览(128)

我有这个脚本循环通过所有网站集〉〉和所有文档库〉〉然后列出文件夹在这种格式“网站名称〉〉列表名称〉〉文件夹名称”:-

$AdminUrl = "https://*****-admin.sharepoint.com/"
Connect-PnPOnline -Url $AdminUrl -Interactive 
 
#sharepoint online get all site collections PowerShell
$SiteColl = Get-PnPTenantSite
 
 
#sharepoint online PowerShell iterate through all site collections
ForEach($Site in $SiteColl)
{
 
   Connect-PnPOnline -Url $Site.Url -Interactive
   $Webs = Get-PnPSubWeb -Recurse -IncludeRootWeb
               
               
    #Get All document libraries
ForEach($Web in $Webs)
    {
               $DocumentLibraries = Get-PnPList -Web $Web | Where-Object {$_.BaseType -eq "DocumentLibrary" -and $_.Title -ne "Site Assets" -and $_.Hidden -eq $false}
    #Iterate through each document library
    ForEach($Library in $DocumentLibraries)
               {
               $f =   Get-PnPFolder -List $Library
               
               ForEach($folder in $f)
               {
               
               Write-Host $Site.Title " --> "  $Library.Title  " --> " $folder.Name
               }
               
               }
}
 
 
}

但我面临的问题是,Get-PnPFolder -List $Library将得到所有的主文件夹和子文件夹..那么我怎么能限制这个命令只得到主文件夹(第一级文件夹)没有子文件夹?
谢谢

编辑以下是更新后的脚本:-

$AdminUrl = "https://***-admin.sharepoint.com/"
Connect-PnPOnline -Url $AdminUrl -Interactive 
 
#sharepoint online get all site collections PowerShell
$SiteColl = Get-PnPTenantSite
$csvOutput = @()
 
#sharepoint online PowerShell iterate through all site collections
ForEach($Site in $SiteColl)
{
 
   Connect-PnPOnline -Url $Site.Url -Interactive
   $Webs = Get-PnPSubWeb -Recurse -IncludeRootWeb
               
               
    #Get All document libraries
ForEach($Web in $Webs)
    {
               $DocumentLibraries = Get-PnPList -Web $Web | Where-Object {$_.BaseType -eq "DocumentLibrary" -and $_.Title -ne "Site Assets" -and $_.Hidden -eq $false}
    #Iterate through each document library
    ForEach($Library in $DocumentLibraries)
               {
               $f =   Get-PnPFolder -List $Library
               
               ForEach($folder in $f)
               {
               
               Write-Host $Site.Title " --> "  $Library.Title  " --> " $folder.Name " --> " $folder.ServerRelativeUrl
            $csvOutput +=  [PsCustomObject]@{SiteTitle = $Site.Title; LibraryTitle = $Library.Title; Folder = $folder.Name; FolderPath = $folder.ServerRelativeUrl}

               }
               
               }
}
 
 
}
$csvOutput | Export-Csv -NoTypeInformation -Path "D:\export123.csv"

并且csvOutput将具有如下值:-

NewRoot  -->  dv  -->  test123  -->  /dv/test123
NewRoot  -->  dv  -->  P100  -->  /dv/P100
NewRoot  -->  dv  -->  WIP  -->  /dv/P100/WIP
NewRoot  -->  Site Pages  -->  Templates  -->  /SitePages/Templates
NewRoot  -->  tagggg  -->  a1  -->  /tagggg/a1
NewRoot  -->  tagggg  -->  a2  -->  /tagggg/a1/a2
NewRoot  -->  testdcdc  -->  test  -->  /testdcdc/test
c2e8gylq

c2e8gylq1#

好的,根据我们的评论,我认为你想做的是这样的。
它遍历整个文件夹列表,但使用哈希表来确保您只收集顶级文件夹的唯一列表。

$AdminUrl = "https://***-admin.sharepoint.com/"
Connect-PnPOnline -Url $AdminUrl -Interactive 
 
#sharepoint online get all site collections PowerShell
$SiteColl   = Get-PnPTenantSite
# create a list to store the results
$csvOutput  = [System.Collections.Generic.List[object]]::new()
# create a Hashtable to eliminate any duplicates
$hashUnique = @{}
 
#sharepoint online PowerShell iterate through all site collections
foreach($Site in $SiteColl) {
    Connect-PnPOnline -Url $Site.Url -Interactive
    $Webs = Get-PnPSubWeb -Recurse -IncludeRootWeb
               
    #Get All document libraries
    foreach($Web in $Webs) {
        $DocumentLibraries = Get-PnPList -Web $Web | 
                             Where-Object {$_.BaseType -eq "DocumentLibrary" -and 
                                           $_.Title -ne "Site Assets" -and 
                                           $_.Hidden -eq $false}
        #Iterate through each document library
        foreach($Library in $DocumentLibraries) {
            Get-PnPFolder -List $Library | ForEach-Object {
                # from the ServerRelativeUrl property, split out the 1st level folder name
                $topFolder = ($_.ServerRelativeUrl -split '/')[1]
                if (!$hashUnique.ContainsKey($topFolder)) {
                    # remember we have already seen this 1st level folder so we don't add it again
                    $hashUnique[$topFolder] = $true
                    Write-Host "$($Site.Title) --> $($Library.Title) --> $topFolder"
                    # add an object to the output list
                    $csvOutput.Add([PsCustomObject]@{
                        SiteTitle    = $Site.Title
                        LibraryTitle = $Library.Title
                        Folder       = $topFolder
                    })
                }
            }
        }
    }
}
# now write the csv
$csvOutput | Export-Csv -NoTypeInformation -Path "D:\FirstLevel Folders.csv"

相关问题