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

[玩转系统] SharePoint Online:使用 PowerShell 获取子网站

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

SharePoint Online:使用 PowerShell 获取子网站


要求:在 SharePoint Online 中使用 PowerShell 获取子网站。

[玩转系统] SharePoint Online:使用 PowerShell 获取子网站

此博文展示了如何使用 PowerShell 在 SharePoint Online 中获取子网站。如果您需要在特定子网站上快速执行某些操作或收集有关特定网站的信息,此脚本会很有用。

在 SharePoint Online 中使用 PowerShell 获取子网站

由于没有 PowerShell cmdlet 可以通过 CSOM 检索 SharePoint Online 子网站,因此以下是如何从 SharePoint Online 中的 PowerShell 获取子网站 (SPWeb):


#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"
    
#Function to Get the Web
Function Get-SPOWeb() 
{
    Param
    (
        $WebURL = $(throw "Please Enter the Site URL:")
    )
    Try {
        #sharepoint online powershell get web
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($WebURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
        $Web = $Ctx.Web
        $Ctx.Load($web)
        $Ctx.executeQuery()
        Return $Web 
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}

#Get Credentials to connect
$Cred = Get-Credential

#powershell cmdlet to get sharepoint online subsite
$Web = Get-SPOWeb -WebURL "https://crescent.sharepoint.com/sites/marketing"

#sharepoint online powershell get site title
Write-host $Web.Title

这相当于 SharePoint 本地 PowerShell 中的 Get-SPWeb cmdlet。

用于获取 SharePoint Online 子网站的 PnP PowerShell cmdlet

从 PnP PowerShell 获取 SharePoint Online 子网站非常简单!使用 Get-PnPWeb PowerShell cmdlet 获取 SharePoint Online 子网站。


#Parameters
$SiteURL ="https://Crescent.sharepoint.com/sites/marketing/2018"

#Connect to PnP Online
Connect-PnPOnline -URL $SiteURL -Interactive 

#Get the Web
$Web = Get-PnPWeb

#Get the title
Write-Host $Web.Title

此 cmdlet 将返回给定 SharePoint Online 网站的 Web(或子网站),从而使您可以轻松使用所需的信息。只需指定您想要获取的站点的 URL,cmdlet 将完成剩下的工作。

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

同样,我们可以使用 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"
    
#Function to Get Subsites of the Web
Function Get-SPOSubSites() 
{
    Param
    (
        $WebURL = $(throw "Please Enter the Site URL:")
    )
    Try {
        #Get subsites of a site (1st Level or immediate childs)
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($WebURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
        $Web = $Ctx.Web
        $Ctx.Load($web)
        $Ctx.Load($web.Webs)
        $Ctx.executeQuery()
        Return $Web.Webs
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}
 
#Get Credentials to connect
$Cred = Get-Credential

#Get All Subsites of the Web
$SubSites = Get-SPOSubSites -WebURL "https://crescent.sharepoint.com/sites/marketing"

#Get the URL of Each subsite
ForEach($Site in $SubSites)
{
    Write-host $Site.Url
}

使用 PnP PowerShell 从 SharePoint Online 网站获取所有子网站非常简单:


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

#Connect to PnP Online
Connect-PnPOnline -URL $SiteURL -Interactive 

#Get subsites of the site
Get-PnPSubWeb

要获取子站点的所有属性,请使用以下命令:Get-PnPSubweb -Identity “Subsite-URL”|选择对象-属性*。这将返回给定子网站的所有属性。要获取有关属性的特定信息,请使用以下命令:Get-PnPSubweb -Identity “Subsite-URL”|选择对象-展开属性标题。这将返回给定子网站的 Title 属性值。

此脚本返回给定站点的所有直接子站点。递归获取所有子站点怎么样?

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

以下是如何在 SharePoint Online 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"
    
#Function to Get all Subsites recursively
Function Get-SPOSubSitesRecursively() 
{
    Param
    (
        $WebURL = $(throw "Please Enter the Site URL:")
    )
    Try {
        $SubSites= @()
        #Get subsites of a site
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($WebURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
        $Web = $Ctx.Web
        $Ctx.Load($Web)
        $Ctx.Load($Web.Webs)
        $Ctx.executeQuery()
        $SubSites += $Web

        #Call the function recursively on each subsite
        ForEach($Web in $Web.Webs)
        {
            $SubSites += Get-SPOSubSitesRecursively $Web.Url
        }
        Return $SubSites
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}
 
#Get Credentials to connect
$Cred = Get-Credential

#Get All Subsites of the Web
$SubSites = Get-SPOSubSitesRecursively -WebURL "https://crescent.sharepoint.com/sites/marketing"

#Get the URL of Each subsite
ForEach($Site in $SubSites)
{
    Write-host $Site.Url
}

若要获取 SharePoint Online 中所有子网站的列表,可以使用 Get-PnPSubweb PnP PowerShell cmdlet。此 cmdlet 将返回指定网站集中所有子网站的列表。您还可以使用 -Recurse 开关从所有子站点获取所有子站点的列表。


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

#Connect to PnP Online
Connect-PnPOnline -URL $SiteURL -Interactive 

#Get all subsites of the site recursively
Get-PnPSubWeb -Recurse

如果您还想包含根站点,请使用:“-IncludeRootWeb”开关:


Get-PnPSubWeb -Recurse -IncludeRootWeb

相关文章:

  • PowerShell 获取 SharePoint Online 网站集中的所有子网站
  • PowerShell 用于列出 SharePoint Online 中的所有网站和子网站

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

取消回复欢迎 发表评论:

关灯