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

[玩转系统] 在 SharePoint 2016 中使用 PowerShell 从 CSV 文件批量创建网站集

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

在 SharePoint 2016 中使用 PowerShell 从 CSV 文件批量创建网站集


PowerShell cmdlet New-SPSite 用于在 SharePoint 中创建新网站集。它采用 URL 和 Owner 强制参数以及其他参数。这是一个例子:

使用 PowerShell 创建 SharePoint 网站集:

要在 SharePoint 中创建网站集,请使用以下 PowerShell 脚本:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Parameters to create new site collection
$SiteName = "Demo"
$SiteURL= "https://intranet.Crescent.com/sites/demo"
$SiteOwner = "Crescent\SPAdmin"
$SiteTemplate = "STS#0"
 
#create site collection with given parameters
New-SPSite -Url $URL -Name $SiteName -OwnerAlias $SiteOwner -Template $SiteTemplate

PowerShell 从 CSV 文件批量创建多个网站集:

您可以使用 PowerShell 从 CSV 文件批量创建 SharePoint 网站集。在这篇博文中,我们将向您展示如何使用 PowerShell 从 CSV 文件在 SharePoint 中创建多个网站集。如果您需要为新项目或计划快速创建多个网站集,这会很有用。

让我们添加一些错误处理并将 New-SPSite 包装到可重用的 PowerShell 函数中,以从 CSV 文件创建多个网站集。

  • 第 1 步: 按以下格式创建 CSV 文件。根据您的要求填充列值。

    [玩转系统] 在 SharePoint 2016 中使用 PowerShell 从 CSV 文件批量创建网站集

  • 步骤 2: 使用此 PowerShell 脚本从 CSV 读取并批量创建网站集。

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Function to Create new Site Collection
Function Create-SPSite
{
  param
    (
        [string]$Name  = $(throw "Please Provide the Site Name!"),
        [string]$URL = $(throw "Please Provide the Site URL!"),
        [string]$Owner = $(throw "Please Provide the Site Owner!"),
        [string]$Template = $(throw "Please Provide the Site Template!")
    )
Try {
   #Set the Error Action
    $ErrorActionPreference = "Stop"
 
    #Check if the site collection exists already
    $SiteExists = Get-SPSite | where {$_.url -eq $URL}
    #Check if site exists in the recycle bin
    $SiteExistsInRecycleBin = Get-SPDeletedSite | where {$_.url -eq $URL}
  
    If($SiteExists -ne $null)
    {
        write-host "Site $($url) exists already!" -foregroundcolor red
    }
    elseIf($SiteExistsInRecycleBin -ne $null)
    {
        write-host "Site $($url) exists in the recycle bin!" -foregroundcolor red
    }
    else
    {
        #create site collection
        New-SPSite -Url $URL -Name $Name -OwnerAlias $Owner -Template $Template
        write-host "Site Collection $($url) Created Successfully!" -foregroundcolor Green
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
    }
Finally {
    #Reset the Error Action to Default
    $ErrorActionPreference = "Continue"
 } 
}
 
#Read from CSV and create site collection
Import-Csv "C:\SitesToCreate.csv" | Foreach-Object { 
   Create-SPSite -Name $_.SiteName -URL $_.SiteURL -Owner $_.SiteOwner -Template $_.SiteTemplate  }

此脚本使用 Import-Csv 和 SharePoint cmdlet 的组合在 SharePoint 中创建多个网站集。还有其他参数,例如内容数据库、语言、站点描述等,您可以与之合并。

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

取消回复欢迎 发表评论:

关灯