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

[玩转系统] SharePoint Online:使用 PowerShell 循环访问网站集中的所有子网站

作者:精品下载站 日期:2024-12-14 21:06:35 浏览:15 分类:玩电脑

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


要求:在使用 PowerShell 使用 SharePoint Online 时,我们经常需要循环访问网站集下的每个子网站。

用于迭代 SharePoint Online 网站集中的所有子网站的 PowerShell CSOM 脚本:

使用 PowerShell 循环访问 SharePoint Online 网站集中的所有子网站对于管理和自动化 SharePoint Online 环境至关重要。循环遍历所有子站点允许您检索有关子站点的信息并执行各种操作,例如创建或删除工件。本文将讨论使用 PowerShell 循环访问 SharePoint Online 网站集中的所有子网站。

此脚本循环访问每个子网站并列出所有子网站 URL:


#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"

#Variables for Processing
$SiteCollUrl = "https://crescent.sharepoint.com/sites/projects"
 
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
#Custom function to get all subsites in a given site collection
Function Get-SPOWebs($Url, $Credentials)
{
    #Set up the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($Url) 
    $Context.Credentials = $credentials

    $web = $Context.Web
    $Context.Load($web)
    $Context.Load($web.Webs)
    $Context.ExecuteQuery()

    #Do Something Here
    Write-host $Web.URL

    #Process Each subsite in current site
    ForEach($web in $web.Webs)
    {
        #call the function recursively
        Get-SPOWebs $web.Url $Credentials
    }
} 

#call the function                   
Get-SPOWebs $SiteCollUrl $Credentials

以下是循环访问网站集中的所有子网站并更改每个子网站上的徽标的实际示例:如何使用 PowerShell 在 SharePoint Online 中更改徽标?

如何使用 PnP PowerShell 遍历 SharePoint Online 中的所有子网站?

使用 Get-PnPSubWeb cmdlet 循环访问子站点的 PnP PowerShell 版本如下所示:


#Parameter
$SiteURL = "https://crescent.sharepoint.com/sites/projects"
 
Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
  
    #Get all subsites in a SharePoint Online site
    $Subsites = Get-PnPSubWeb -Recurse -IncludeRootWeb
      
    #Iterate through each subsite
    ForEach($site in $Subsites)
    {
        Write-host $Site.Url
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

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

取消回复欢迎 发表评论:

关灯