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

[玩转系统] SharePoint Online:使用 PowerShell 获取网站集中的所有子网站

作者:精品下载站 日期:2024-12-14 14:11:58 浏览:14 分类:玩电脑

SharePoint Online:使用 PowerShell 获取网站集中的所有子网站


要求: PowerShell 列出 SharePoint Online 网站集中的所有子网站。

[玩转系统] SharePoint Online:使用 PowerShell 获取网站集中的所有子网站

如何获取 SharePoint Online 网站中的子网站列表?

您可以通过多种不同的方式列出 SharePoint Online 网站集中的所有子网站。一种选择是使用网站内容页面。

  1. 导航到站点>>单击设置>>选择“站点内容”。
  2. 在网站内容页面中,单击“子网站”(URL 快捷方式:/_layouts/15/viewlsts.aspx?view=15)。此页面将显示当前网站集中所有子网站的列表。

    [玩转系统] SharePoint Online:使用 PowerShell 获取网站集中的所有子网站

但是,此页面仅列出该网站的直接子网站!如果有任何二级子网站,您将不会在此页面上看到它们。

SharePoint Online:使用 PowerShell 获取所有子网站

如何获取 SharePoint Online 中网站集的所有子网站?作为 SharePoint 管理员,您可能需要获取 SharePoint Online 环境中所有子网站的列表,这可以使用 PowerShell 来完成。本博文将向您展示如何使用 PowerShell 获取 SharePoint Online 网站中所有子网站的列表。

要获取站点的所有子站点(顶级站点或子站点),请使用以下 PowerShell 脚本:


#Requirement: sharepoint online get all subsites powershell
#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
$SiteURL = "https://crescent.sharepoint.com/Sites/Marketing"

Try {
    #Get Credentials to connect
    $Cred= Get-Credential

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Get the web
    $Web = $Ctx.web
    $Ctx.Load($Web)
    
    #get all subsites in sharepoint online powershell
    $Ctx.Load($Web.Webs)
    $Ctx.executeQuery()

    #Loop through each subsite and get subsite URL
    ForEach ($Subweb in $web.Webs)  
    {
       Write-host $Subweb.url  
    }  
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

此 PowerShell 脚本获取给定站点的所有直接子站点(或第一级子站点)。如何递归列出所有子站点?

PowerShell 获取网站集中的所有子网站

以下是递归获取所有子网站 (Web) 的 SharePoint Online PowerShell:


#Load SharePoint Online 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
$SiteUrl = "https://crescent.sharepoint.com/sites/Sales/"
$UserName="[email protected]"
$Password ="Password goes here"
 
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
 
#sharepoint online powershell get subsites
Function Get-SPOWeb() {
param(
    $WebURL = $(throw "Please Enter the Site Collection URL")
)

    #Get Web information and subsites
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($webURL)
    $Context.Credentials = $credentials
    $Web = $context.Web
    $Context.Load($web)

    #powershell cmdlet to retrieve sharepoint online subsites
    $Context.Load($web.Webs) 
    $Context.executeQuery()

    #Do something with the current sub-site
    Write-host $Web.URL

    #Iterate through each subsite in the current web
    foreach ($Subweb in $web.Webs)
    {
        #Call the function recursively to process all subsites underneath the current web
        Get-SPOWeb($Subweb.url)
    }
}

#Call the function
Get-SPOWeb -WebURL $SiteUrl

此 PowerShell 从给定网站集中获取所有子网站。如果您需要网站集中所有子网站的报告,或者需要立即对所有子网站执行某些操作,这会很有用。例如,迭代所有子网站并更改徽标:SharePoint Online:如何使用 PowerShell 更改网站徽标?

使用 PowerShell PnP 获取站点的子站点 cmdlet Get-PnPSubWeb

我们也可以使用 PowerShell Pnp 获取 SharePoint Online 中的子网站。下面是用于获取 SharePoint Online 网站集中的所有子网站的 PnP PowerShell。


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred

    #Get subsites in SharePoint Online using PnP PowerShell
    $WebsCollection = Get-PnPSubWeb -Recurse
    
    #Iterate through each subsite
    ForEach($Web in $WebsCollection)
    {
        Write-host $Web.Url
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Get-PnPSubWeb cmdlet 中的 -Recurse 开关以递归方式获取网站集各级的所有子网站;如果要包含网站集的根网站,请使用:-IncludeRootWeb 开关。


#Parameter
$SiteURL = "https://crescent.sharepoint.com/sites/marketing" 

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
 
    #Get all webs in SharePoint Online site using PnP PowerShell
    $WebsCollection = Get-PnPSubWeb -Recurse -IncludeRootWeb
     
    #Iterate through each web in site
    ForEach($Web in $WebsCollection)
    {
        Write-host $Web.Url
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

若要使用 PowerShell 获取 SharePoint Online 中的所有网站和子网站,请使用:使用 PowerShell 获取 SharePoint Online 中的所有网站和子网站

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

取消回复欢迎 发表评论:

关灯