You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell遍历SharePoint文件夹脚本报错及深层文件夹检测异常问题

PowerShell脚本问题修复方案

问题根因说明

  • 问题1(Export-Csv参数为null报错):
    1. 执行顺序错误:你先执行$results | Export-Csv,再调用GetFolders函数生成结果,导出时$results还未被赋值
    2. 变量作用域错误:$results定义在GetFolders函数内部,属于局部变量,函数外部无法访问,且每次递归都会重新初始化$results = @()覆盖之前的采集结果
  • 问题2(3-5级含通配符文件夹未被检测到):
    1. 逻辑运算符优先级错误:原判断条件$Item["FileRef"] -cnotlike "*archief*" -or $Item["FileLeafRef"] -cnotlike "*archief*" -and ($Item["File_x0020_Type"])中-and优先级高于-or,判断逻辑完全不符合需求,且你需要的是路径和文件名都不含archief,应该用-and而不是-or
    2. 遍历逻辑冗余错误:你一边递归遍历文件夹,一边每次循环都拉取整个文档库的所有文件,没有和当前遍历的文件夹做关联,导致深层文件夹的过滤逻辑失效
    3. 列表过滤逻辑错误:直接对列表对象$DocLib使用-cnotlike判断无效,应该判断列表的标题/根文件夹名称

修正后完整脚本

Import-Module PnP.PowerShell

#Config Parameters
$SiteURL= "*******/sites/Archief"
$Folder = "Gedeelde Documenten"
$Pagesize = 500
Connect-PnPOnline -Url $SiteURL -Interactive ### Change to your specific site ###

#Output path
$outputPath = "C:\users\$env:USERNAME\Desktop\test.csv"

# 全局结果数组,定义在函数外部
$results = @()

Function GetFolders($folderUrl)  
{      
    $folderColl=Get-PnPFolderItem -FolderSiteRelativeUrl $folderUrl -ItemType Folder   
    # Loop through the folders  
    foreach($folder in $folderColl)  
    {                      
        $newFolderURL= $folderUrl+"/"+$folder.Name
        if ($newFolderURL -cnotlike "*archief*"){
            write-host -ForegroundColor Green $folder.Name " - " $newFolderURL
            # 仅获取当前文件夹下的文件,不需要每次拉全库
            $fileColl = Get-PnPFolderItem -FolderSiteRelativeUrl $newFolderURL -ItemType File
            foreach($file in $fileColl){
                $fileRef = $file.ServerRelativeUrl
                $fileName = $file.Name
                $fileExt = $file.Extension
                if ($fileRef -cnotlike "*archief*" -and $fileName -cnotlike "*archief*" -and $fileExt){
                    Write-Host "File found. Path:" $fileRef -ForegroundColor Green               
                    # 追加到全局结果数组
                    $script:results += New-Object PSObject -Property @{
                        Path          = $fileRef
                        FileName      = $fileName
                        FileExtension = $fileExt
                    }
                }
            }
           # 递归遍历子文件夹
           GetFolders($newFolderURL)
        }
    }           
}

# 先执行遍历采集数据
GetFolders($Folder)

# 采集完成后再导出CSV
$results | Export-Csv -Path $outputPath -NoTypeInformation -Encoding UTF8

优化说明

如果不需要严格按文件夹递归遍历,也可以直接拉取整个站点所有文档库的文件直接过滤,执行效率更高,不需要递归逻辑:

# 简化版逻辑(无需递归)
Import-Module PnP.PowerShell
$SiteURL= "*******/sites/Archief"
$outputPath = "C:\users\$env:USERNAME\Desktop\test.csv"
Connect-PnPOnline -Url $SiteURL -Interactive

$results = @()
# 获取所有文档库
$DocLibs = Get-PnPList | Where-Object {$_.BaseTemplate -eq 101 -and $_.Title -cnotlike "*archief*" -and $_.RootFolder.Name -cnotlike "*archief*"}
foreach($DocLib in $DocLibs){
    $AllItems = Get-PnPListItem -List $DocLib -Fields "FileRef", "File_x0020_Type", "FileLeafRef" -PageSize 500
    foreach($Item in $AllItems){
        # 仅处理文件,排除文件夹
        if($Item.FileSystemObjectType -eq 'File'){
            if($Item["FileRef"] -cnotlike "*archief*" -and $Item["FileLeafRef"] -cnotlike "*archief*" -and $Item["File_x0020_Type"]){
                $results += New-Object PSObject -Property @{
                    Path          = $Item["FileRef"]
                    FileName      = $Item["FileLeafRef"]
                    FileExtension = $Item["File_x0020_Type"]
                }
            }
        }
    }
}
$results | Export-Csv -Path $outputPath -NoTypeInformation -Encoding UTF8

内容的提问来源于stack exchange,提问作者BartTechne

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 09:15:04