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

获取网站集所有列表及5000条列表项的PowerShell脚本需求

调整后的PowerShell脚本:获取网站集所有列表并筛选大列表

我来帮你修改这个脚本,让它能遍历网站集里的所有站点(包括根站点和所有子站点),获取每个站点的所有列表,还能自动挑出那些有5000条及以上项的大列表。下面是完整可运行的版本:

# 环境变量配置
$username = "user.name@xxx.com"
$password = Read-Host -Prompt "Enter your password: " -AsSecureString
$siteCollectionUrl = "https://xxx.sharepoint.com/sites/xxxxxx"

# 转换密码格式
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)

# 加载SharePoint Online DLL(如果已经装了SharePoint Online管理模块,这两行可以省略)
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

# 初始化客户端上下文
$context = New-Object Microsoft.SharePoint.Client.ClientContext($siteCollectionUrl)
$context.Credentials = $credentials

try {
    # 获取网站集的根站点和所有子站点
    $web = $context.Web
    $context.Load($web)
    $context.Load($web.Webs)
    $context.ExecuteQuery()

    # 用来存所有列表和大列表的数组
    $allSiteCollectionLists = @()
    $largeLists = @()

    # 先处理根站点的列表
    ProcessWebLists -web $web -context $context -allListsArray ([ref]$allSiteCollectionLists) -largeListsArray ([ref]$largeLists)

    # 递归处理所有子站点的列表(不管嵌套多少层都能抓到)
    ProcessChildWebs -webs $web.Webs -context $context -allListsArray ([ref]$allSiteCollectionLists) -largeListsArray ([ref]$largeLists)

    # 输出结果,用表格看着更清楚
    Write-Host "`n=== 网站集下所有列表 ==="
    $allSiteCollectionLists | Format-Table Title, Url, ItemCount -AutoSize

    Write-Host "`n=== 包含5000条及以上项的大列表 ==="
    $largeLists | Format-Table Title, Url, ItemCount -AutoSize
}
catch {
    Write-Host "执行出错: $_" -ForegroundColor Red
}
finally {
    # 用完记得清理上下文
    $context.Dispose()
}

# 处理单个站点列表的函数
function ProcessWebLists {
    param(
        [Microsoft.SharePoint.Client.Web]$web,
        [Microsoft.SharePoint.Client.ClientContext]$context,
        [ref]$allListsArray,
        [ref]$largeListsArray
    )

    $context.Load($web.Lists)
    $context.ExecuteQuery()

    foreach ($list in $web.Lists) {
        # 只加载需要的属性,别加载没用的,这样脚本跑起来更快
        $context.Load($list, 'Title', 'Url', 'ItemCount')
        $context.ExecuteQuery()

        # 把列表信息存到总列表数组里
        $allListsArray.Value += [PSCustomObject]@{
            Title     = $list.Title
            Url       = $siteCollectionUrl + $list.Url
            ItemCount = $list.ItemCount
        }

        # 检查是不是大列表,是的话单独存起来
        if ($list.ItemCount -ge 5000) {
            $largeListsArray.Value += [PSCustomObject]@{
                Title     = $list.Title
                Url       = $siteCollectionUrl + $list.Url
                ItemCount = $list.ItemCount
            }
        }
    }
}

# 递归处理子站点的函数,嵌套子站点也能覆盖到
function ProcessChildWebs {
    param(
        [Microsoft.SharePoint.Client.WebCollection]$webs,
        [Microsoft.SharePoint.Client.ClientContext]$context,
        [ref]$allListsArray,
        [ref]$largeListsArray
    )

    foreach ($childWeb in $webs) {
        $context.Load($childWeb)
        $context.Load($childWeb.Webs)
        $context.ExecuteQuery()

        # 先处理当前子站点的列表
        ProcessWebLists -web $childWeb -context $context -allListsArray $allListsArray -largeListsArray $largeListsArray

        # 如果这个子站点还有子站点,继续递归处理
        if ($childWeb.Webs.Count -gt 0) {
            ProcessChildWebs -webs $childWeb.Webs -context $context -allListsArray $allListsArray -largeListsArray $largeListsArray
        }
    }
}

关键改动说明

  • 遍历所有站点:加了递归函数ProcessChildWebs,不管网站集里有多少层嵌套的子站点,都能挨个遍历到,不会漏掉任何一个站点的列表。
  • 性能优化:只加载列表的Title、Url、ItemCount三个必要属性,避免加载一堆没用的数据,脚本运行速度会快很多。
  • 大列表筛选:用ItemCount属性直接判断列表项数量,不用加载所有列表项(要是真加载5000+条项,那速度慢得离谱),高效又准确。
  • 清晰输出:把所有列表和大列表分开输出,用表格格式展示,看着一目了然。

小提醒

  1. 确保你的电脑上装了SharePoint Online的客户端组件,要是没装,装个SharePoint Online Management Shell就能拿到需要的DLL文件了。
  2. 要是DLL路径不对,记得改Add-Type里的路径,一般默认路径就是C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\。
  3. 运行脚本的账号得有网站集里所有站点的访问权限,不然会弹出权限不足的错误哦。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 11:06:12