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

[玩转系统] SharePoint Online:PowerShell 迭代所有网站集

作者:精品下载站 日期:2024-12-14 21:30:34 浏览:13 分类:玩电脑

SharePoint Online:PowerShell 迭代所有网站集


要求:PowerShell 可循环访问 SharePoint Online 租户中的所有网站集。

[玩转系统] SharePoint Online:PowerShell 迭代所有网站集

如何循环访问 SharePoint Online 中的所有网站?

您是否曾经想要循环浏览 SharePoint Online 环境中的所有网站?也许您想要获取所有网站所有者的列表,或者只是枚举所有网站,以便您可以对它们进行进一步的处理。无论什么原因,PowerShell 都可以提供帮助!在本文中,我们将了解如何使用 PowerShell 循环浏览 SharePoint Online 租户中的所有网站。

使用 PowerShell 循环访问 SharePoint Online 中的所有网站

有不同的方法可以做到这一点。让我们使用 SharePoint Online PowerShell cmdlet Get-SPOSite 检索租户中的所有网站,然后迭代它们。


#Parameter
$AdminSiteURL = "https://crescent-admin.sharepoint.com"

#Connect to SharePoint Online Admin Center
Connect-SPOService -Url $AdminSiteURL
 
#Get All site collections
$SiteCollections = Get-SPOSite -Limit All
 
#Traverse through each site collection and get their subsits
Foreach ($Site in $SiteCollections)
{
    Write-Host $Site.Url
}

此脚本首先使用 Connect-SPOService cmdlet 连接到 SharePoint Online,并提示您输入登录凭据。然后,它使用 Get-SPOSite cmdlet 获取租户中的所有网站集。最后,它循环访问每个网站集并执行操作(在本例中为获取网站 URL)。这对于导出站点数据或仅获取场中所有站点的列表等任务非常有用。

SharePoint Online:PnP PowerShell 循环访问网站

此 PnP PowerShell 脚本循环访问 SharePoint Online 环境中的所有网站并获取网站 URL。


#Parameter
$AdminSiteURL = "https://crescent-admin.sharepoint.com"

Try {
    #Connect to Admin Center
    Connect-PnPOnline -Url $AdminSiteURL -Interactive
 
    #Get All Site collections  
    $SitesCollection = Get-PnPTenantSite
 
    #Loop through each site collection
    ForEach($Site in $SitesCollection)  
    {  
        Write-host -F Green $Site.Url  
    }
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

您可以修改脚本以对每个网站集执行所需的任何操作。例如,您可以使用 Set-PnPWeb cmdlet 更新站点的主题或导航,或使用 Add-PnPUserToGroup cmdlet 将用户添加到组。

迭代所有站点 - 排除某些站点类型

排除 Seach Center、Mysite Host、重定向站点、应用程序目录、内容类型中心、电子数据展示和机器人站点并将所有站点置于“/sites/”托管路径下怎么样?


#Config Variables
$TenantAdminURL = "https://crescent-admin.sharepoint.com"

#Get Credentials to connect
$Cred = Get-Credential

#Connect to Admin Center using PnP Online
Connect-PnPOnline -Url $TenantAdminURL -Credentials $Cred

#Get All Site collections - Exclude: Seach Center, Redirect sites, Mysite Host, App Catalog, Content Type Hub, eDiscovery and Bot Sites
$SiteCollections = Get-PnPTenantSite | Where { $_.URL -like '*/sites*' -and $_.Template -NotIn ("SRCHCEN#0", "REDIRECTSITE#0", "SPSMSITEHOST#0", "APPCATALOG#0", "POINTPUBLISHINGHUB#0", "EDISC#0", "STS#-1")}

#Loop through each site collection
ForEach($Site in $SiteCollections)
{
    Try {
        Write-host "Processing Site:"$Site.URL -f Yellow
        #Connect to the site
        Connect-PnPOnline -Url $Site.URL -Credentials $Cred

        #Get all document libraries from the site
        $DocumentLibraries = Get-PnPList | Where-Object {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $False -and $_.ItemCount -gt 0}

        #Iterate through document libraries
        ForEach ($List in $DocumentLibraries)
        {
            Write-host "`tTotal Number of Items in '$($List.Title)':" $List.itemCount -f Green
        }
    }
    Catch {
        write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
    }
}

如果您需要获取所有站点的列表,这会很方便。

用于迭代所有网站集的 PowerShell CSOM 脚本

请注意,CSOM 方法仅获取经典站点(此方法不包含现代站点或通信站点!)


#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"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll"
 
#Set Parameters
$AdminCenterURL = "https://crescent-admin.sharepoint.com/"

Try {
    #Setup Credentials to connect
    $Cred= Get-Credential
     
    #Setup the Context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminCenterURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Get the tenant object 
    $Tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($Ctx)
 
    #Get all site collections
    $Sites= $Tenant.GetSiteProperties(0, $true)
    $Ctx.Load($Sites)
    $Ctx.ExecuteQuery()
     
    ForEach($Site in $Sites)
    {
        Write-Host $Site.URL
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

相关文章:SharePoint Online:使用 PowerShell 获取所有网站集和子网站

结论

总之,使用 PowerShell 是迭代 SharePoint Online 中的所有网站集并对其执行操作的便捷方法。通过使用本教程中提供的脚本,您可以轻松连接到 SharePoint Online,获取租户中所有网站集的列表,并迭代每个网站集以执行特定操作。当您需要对多个网站集执行批处理操作,或者当您想要自动执行管理 SharePoint Online 环境的过程时,这非常有用。

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

取消回复欢迎 发表评论:

关灯