PowerShell 获取 SharePoint Online 站点信息

  前言

  最近,用户想要查一些站点的信息,当然,如果个别站点那就非常容易了,批量的话你懂的。

  正文

  1.这是PowerShell 命令执行成功的页面,命令附后,如下图:

image

  2.这是获取一个站点的信息,如果你想获取多个,稍微改一下,就好了,如下图:

$siteUrl = "https://linyus.sharepoint.com/sites/Develop"  # 替换为你的SPO站点地址

if (-not (Get-Module -ListAvailable -Name PnP.PowerShell)) {
    Write-Host "正在安装最新版PnP.PowerShell..." -ForegroundColor Cyan
    Set-PSRepository -Name PSGallery -InstallationPolicy Trusted -ErrorAction SilentlyContinue
    Install-Module -Name PnP.PowerShell -Force -AllowClobber -Scope CurrentUser -ErrorAction Stop
}

Import-Module PnP.PowerShell -DisableNameChecking -Force -ErrorAction Stop
Write-Host "✅ PnP.PowerShell模块加载成功`n" -ForegroundColor Green

$isConnected = $false

try {
    Write-Host "🔗 正在连接站点: $siteUrl" -ForegroundColor Cyan
    Connect-PnPOnline -Url $siteUrl -UseWebLogin -ErrorAction Stop
    $isConnected = $true
    Write-Host "✅ 站点连接成功`n" -ForegroundColor Green

    Write-Host "📊 开始获取站点信息...`n" -ForegroundColor Cyan

    $web = Get-PnPWeb -ErrorAction Stop
    $site = Get-PnPSite -ErrorAction Stop

    $allLists = Get-PnPList -ErrorAction Stop
    $documentLibraries = $allLists | Where-Object { $_.BaseTemplate -eq 101 }  # 101=文档库
    $customLists = $allLists | Where-Object { $_.BaseTemplate -eq 100 }      # 100=自定义列表

    Write-Host "========== SharePoint Online 站点核心信息 ==========" -ForegroundColor Green
    [PSCustomObject]@{
        "站点标题"         = $web.Title
        "站点URL"          = $web.Url
        "列表/库总数"      = $allLists.Count
        "文档库数量"       = $documentLibraries.Count
        "自定义列表数量"   = $customLists.Count
    } | Format-List

    Write-Host "`n========== 站点内前5个列表/库(名称+类型) ==========" -ForegroundColor Green
    $allLists | Select-Object Title, BaseTemplate -First 5 | Format-Table -AutoSize

}
catch {
    Write-Host "`n❌ 执行出错: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}
finally {
    # 修复连接检测逻辑:仅在确认连接成功时断开
    if ($isConnected) {
        try {
            Disconnect-PnPOnline -ErrorAction SilentlyContinue
            Write-Host "`n🔌 已断开SPO站点连接" -ForegroundColor Cyan
        }
        catch {
            Write-Host "`n⚠️ 断开连接时出现小问题,可忽略: $($_.Exception.Message)" -ForegroundColor Yellow
        }
    }
}

  结束语

  在管理SharePoint Online站点的时候,PowerShell是一个非常好用的工具,尤其是批量建表、处理数据等。

posted @ 2026-02-28 22:16  霖雨  阅读(19)  评论(0)    收藏  举报