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

[玩转系统] 使用 PowerShell 将母版页上传到 SharePoint

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

使用 PowerShell 将母版页上传到 SharePoint


要求:我们有来自第三方顾问的 SharePoint 2013 母版页,并且希望将该母版页复制应用到 SharePoint 网站。

用于将母版页上传到 SharePoint 的 PowerShell 脚本:

您是否曾经在 SharePoint 中创建过母版页并将其上传到您的网站?如果有一种简单的方法将母版页上传到 SharePoint 而不是手动执行,岂不是很好?嗯,有!这篇博文将向您展示如何使用 PowerShell 上传母版页。


Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

$WebURL = "https://intranet.crescent.com/"
$MasterPage = "Crescentv1.master"
$SourcePath ="D:\Branding\MasterPages\Crescentv1.master"

#Get the Web
$web = Get-SPWeb $WebURL

#Get the Target folder - Master page Gallery
$MasterPageList = $web.GetFolder("Master Page Gallery")

#Set the Target file for Master page
$TargetPath = $Web.Url + "/_catalogs/masterpage/Crescentv1.master"

#Get the Master page from local disk
$MasterPageFile = (Get-ChildItem $SourcePath).OpenRead()

 #Check if file exist already
 if ($Web.GetFile($TargetPath).Exists)
  {
    $Web.GetFile($TargetPath).recycle()
  }

#upload master page using powershell
$MasterPage = $MasterPageList.Files.Add($TargetPath,$MasterPageFile,$false)
$web.Update()

此脚本将母版页从本地磁盘复制到 SharePoint。只需将代码包装在循环内即可将母版页复制到所有站点。上传后,我们可以使用另一个 PowerShell 脚本设置母版页:使用 PowerShell 在 SharePoint 中更改母版页

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

取消回复欢迎 发表评论:

关灯