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

[玩转系统] SharePoint Online:使用 PowerShell 从 CSV 文件创建多个子网站

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

SharePoint Online:使用 PowerShell 从 CSV 文件创建多个子网站


要求:使用 PowerShell 在 SharePoint Online 中创建多个子网站。

SharePoint Online:如何使用 PowerShell 创建多个子网站?

在 SharePoint Online 中创建多个子网站可能是一个乏味的过程,如果您必须一个接一个地完成,则可能需要大量工作。如果有一种方法可以自动化该过程不是很好吗?幸运的是,有一个 PowerShell 脚本可以使该过程变得更加容易。我将在这篇文章中向您展示如何使用 PowerShell 批量创建子网站。

我的另一篇文章中解释了使用 PowerShell 在 SharePoint Online 中创建子网站:如何在 SharePoint Online 中使用 PowerShell 创建子网站?,您可以通过简单的数组和循环在 SharePoint Online 中创建多个子网站,如下所示:


#Site collection URL
$SiteURL = "https://crescent.sharepoint.com/sites/intranet"

#Connect to Pnp Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Define subsites in array
$Subsites = "Sales","Production", "Marketing", "Support"

#Iterate through each site
ForEach($Site in $Subsites)
{
    #Create subsite
    New-PnPWeb -Title $Site -Url $Site -InheritNavigation -Template STS#3
}

如果您需要创建大量子网站并且不想手动执行此操作,这会很有帮助。

PowerShell 在 SharePoint Online 中从 CSV 文件创建子网站

按以下格式创建 CSV 文件。根据您的要求填充列值。对于每个子站点,设置以下属性:

  1. SiteCollectionURL - 子网站的父网站集
  2. SubSiteTitle - 子网站的名称
  3. 描述 - 子网站描述
  4. 网址 - 子网站网址
  5. BreakInheritance - 默认情况下,子网站继承父网站的权限
  6. 模板 - 子网站模板

这是我的 CSV 文件

[玩转系统] SharePoint Online:使用 PowerShell 从 CSV 文件创建多个子网站

您可以在此处下载 CSV 文件:

PowerShell 从 CSV 创建子网站

此 PnP PowerShell 脚本读取 CSV 文件,并根据 CSV 中的详细信息在给定网站集上创建子网站。


#Function to Create Subsite
Function Create-PnPSubsite
{
  param
    (
        [Parameter(Mandatory=$true)] [string]$SiteCollectionURL  = $(throw "Please Enter the Site Collection URL!"),
        [Parameter(Mandatory=$true)] [string]$Title  = $(throw "Please Enter the Subsite Title!"),
        [Parameter(Mandatory=$true)] [string]$URL = $(throw "Please Enter the Subsite URL!"),
        [Parameter(Mandatory=$false)] [string]$Description = [string]::Empty,
        [Parameter(Mandatory=$false)] [string]$BreakInheritance = "False",
        [Parameter(Mandatory=$true)] [string]$Template = $(throw "Please Provide the Site Template!")
    )
    Try{
        #Connect to PnP Online
        Connect-PnPOnline -Url $SiteCollectionURL -Credentials $Cred #-Interactive

        #Check if subsite exists
        $Subsite = Get-PnPWeb -Identity $URL -ErrorAction SilentlyContinue
        If($Subsite -eq $null) 
        {
            #Create subsite
            $BreakInheritanceFlag = [System.Convert]::ToBoolean($BreakInheritance) 
            $SubSite = New-PnPWeb -Title $Title -Url $URL -Description $Description -BreakInheritance:$BreakInheritanceFlag -Template $Template 
            Write-Host "Created Subsite:"$URL -ForegroundColor Green
        }
        Else
        {
            Write-Host "Subsite already exist:"$URL -ForegroundColor Yellow
        }
    }
    catch {
        write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
    }
}

#Set CSV File path
$CSVFilePath = "C:\Temp\Subsites.csv"

#Get Credentials to connect
$Cred = Get-Credential

#Read from CSV and call the function to create subsites
Import-Csv $CSVFilePath | ForEach-Object {
   Create-PnPSubsite -SiteCollectionURL $_.SiteCollectionURL -Title $_.SubSiteTitle -URL $_.URL -Description $_.Description -BreakInheritance $_.BreakInheritance -Template $_.Template 
}

要创建具有唯一权限的子网站,请使用:如何使用 PowerShell 创建具有唯一权限的 SharePoint Online 子网站?

总之,使用 PowerShell 在 SharePoint Online 中创建多个子网站是自动化创建批量子网站过程的有效方法!此外,通过利用 PowerShell 脚本、CSV 文件和循环,您可以一次创建多个子网站,从而节省时间。

经常问的问题:

如何在 SharePoint Online 中启用子网站创建?

在现代 SharePoint Online 网站中,默认情况下禁用子网站创建。要启用子网站创建:登录 SharePoint Online 管理中心 >> 单击“设置”>> 单击“经典设置页面”。在经典设置页面的“子网站创建”下,选择“显示所有网站的子网站命令”以在所有网站上启用 SharePoint 子网站创建。
详细信息:在 SharePoint Online 中启用子网站创建

如何删除 SharePoint Online 中的子网站?

要从 SharePoint Online 删除子网站: 单击“网站设置”>> 选择“网站设置”。单击“网站操作”组下的“删除此网站”链接。单击“删除”按钮并确认子网站删除,您将看到“您的网站已被删除”。消息一段时间。
更多信息:删除 SharePoint Online 中的子网站

如何在 SharePoint Online 中恢复已删除的子网站?

要恢复子站点,请按照下列步骤操作: 导航到站点设置 >> 单击“回收站” >> 单击“第二阶段回收站”链接,然后通过单击子站点的复选框选择所需的子站点,单击功能区中的“还原”按钮可开始还原子网站。
详细信息:还原 SharePoint Online 中的子网站

如何更改 SharePoint Online 中的子网站 URL?

有两种不同的方法可以做到这一点,我将向您展示两种方法。第一种方法是最简单的,通过 Web 浏览器界面进行,第二种方法是使用 PowerShell。
详细信息:使用 PowerShell 更改子网站 URL SharePoint Online

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

取消回复欢迎 发表评论:

关灯