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

[玩转系统] 如何使用 PowerShell 在 SharePoint Online 中启用自定义脚本?

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

如何使用 PowerShell 在 SharePoint Online 中启用自定义脚本?


要求:允许在 SharePoint Online 中自定义脚本!

默认情况下,出于安全原因,自定义脚本在 SharePoint Online 新式团队网站、通信网站、根网站、用户自行创建的网站以及 OneDrive 网站中处于禁用状态。因此,当禁用自定义脚本时,以下功能将被阻止:

  • 许多 Web 部件(包括内容编辑器和脚本编辑器)均被禁用。
  • 上传可能包含脚本的文件将被阻止。
  • 不允许更改 SharePoint Designer 脚本,并且自定义操作不再起作用。新建子网站和删除网站会将您带到浏览器中的“网站设置”页面。
  • 将网站另存为模板、列出模板、将文档库另存为模板链接在 SharePoint 网站中已禁用。
  • 解决方案库、沙盒解决方案、主题库和 HTML 字段安全性在站点设置中不可用。
  • 默认情况下会阻止以下任何文件类型: .asmx .ascx .aspx .htc .jar .master .swf .xap .xsf
  • 所有其他与脚本相关的功能均被关闭。

因此,为了解决上述问题,我们需要启用自定义脚本设置。本文将引导我们完成在 SharePoint Online 中启用自定义脚本的步骤。

如何通过管理中心在 SharePoint Online 中启用自定义脚本?

如何在 SharePoint Online 中启用自定义脚本?要在 SharePoint Online 中允许自定义脚本,请按照下列步骤操作:

  1. 通过浏览器登录 Microsoft 365 并转到 SharePoint 管理中心:https://-admin.sharepoint.com
  2. 单击左侧导航中的“设置”>> 单击“经典设置页面”(https://-admin.sharepoint.com/_layouts/15/online/TenantSettings.aspx)>>向下滚动到“自定义脚本”部分
  3. 在“自定义脚本”部分下,设置“允许用户在个人站点上运行自定义脚本”和“允许用户在自助创建的站点上运行自定义脚本”选项。

    [玩转系统] 如何使用 PowerShell 在 SharePoint Online 中启用自定义脚本?

  4. 单击“确定”保存更改。
在现代管理中心,此配置已移至“经典设置页面”!单击“设置”>>单击页面底部的“经典设置页面”链接!

这将在 SharePoint Online 租户设置中以及任何其他自助服务网站或管理员创建的网站中启用自定义脚本。不过,此更改最多可能需要 24 小时才能体现。要立即在特定网站集上启用脚本编写,请通过 SharePoint Online Management Shell 使用此 PowerShell 脚本。

在 SharePoint Online 中启用自定义脚本是一个相对简单的过程。通过 SharePoint Online 管理中心启用自定义脚本时,将为租户中的所有网站启用它。而您可以通过 PowerShell 在特定 SharePoint Online 网站集上启用或禁用自定义脚本。

允许使用 PowerShell 在 SharePoint Online 中自定义脚本

如何在 SharePoint Online 网站中启用自定义脚本?让我们看一下在 SharePoint Online 中启用自定义脚本的步骤。您需要做的第一件事是打开 SharePoint Online Management Shell 并使用“Connect-SPOService -url https://yourtenant-admin.sharepoint.com”cmdlet 连接到您的 SharePoint Online 租户。连接后,您需要运行以下 cmdlet:Set-SPOsite -DenyAddAndCustomizePages 0

以下是如何在网站集级别使用 PowerShell 在 SharePoint Online 中启用自定义脚本:


#Config Parameters
$AdminSiteURL="https://crescent-admin.sharepoint.com"
$SiteURL="https://crescent.sharepoint.com"  
 
#Connect to SharePoint Online Tenant Admin portal
Connect-SPOService -URL $AdminSiteURL
  
#sharepoint online enable custom scripts powershell - Disable DenyAddAndCustomizePages Flag
Set-SPOSite $SiteURL -DenyAddAndCustomizePages $False

要禁用 SharePoint Online 网站集的自定义脚本,请在 PowerShell 控制台中使用以下命令:


Set-SPOsite $SiteURL -DenyAddAndCustomizePages $True

PnP PowerShell 在 SharePoint Online 中启用自定义脚本

要为 SharePoint 网站集启用自定义脚本,我们必须使用 SharePoint Online 中的 PnP PowerShell 模块设置“DenyAddandCustomizepages”标志:


#Parameters
$TenantAdminURL= "https://crescent-admin.sharepoint.com"
$SiteURL= "https://crescent.sharepoint.com/sites/Procurement"

#Connect to Tenant Admin
Connect-PnPOnline $TenantAdminURL -Interactive

#Enable Custom Scripting by turning OFF Deny Flag
Set-PnPTenantSite -Identity $SiteURL -DenyAddAndCustomizePages:$false

您还可以使用 PnP PowerShell Set-PnPSite cmdlet 在 SharePoint Online 中启用自定义脚本:


#Set Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/purchase"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#sharepoint online allow custom scripts powershell 
Set-PnPSite -Identity $SiteURL -NoScriptSite $false

如何为 SharePoint Online 中的所有网站启用自定义脚本?

如何立即为租户中的所有 SharePoint Online 站点启用自定义脚本,而不是逐个站点执行该脚本?


#Parameters
$TenantURL =  "https://Crescent.sharepoint.com"
 
#Get Credentials to connect
$Credential = Get-Credential
 
#Frame Tenant Admin URL from Tenant URL
$TenantAdminURL = $TenantURL.Insert($TenantURL.IndexOf("."),"-admin")

#Connect to Admin Center
Connect-PnPOnline -Url $TenantAdminURL -Credentials $Credential
 
#Get All Site collections - Filter BOT and MySite Host
$Sites = Get-PnPTenantSite -Filter "Url -like '$TenantURL' -and Url -notlike 'portals/hub'"

#Iterate through all sites
$Sites | ForEach-Object {
    Write-host "Processing Site Collection:"$_.URL

    #Enable Custom Script
    If ($_.DenyAddAndCustomizePages -ne "Disabled")
    {
        Set-PnPTenantSite -Identity $_.URL -DenyAddAndCustomizePages:$false
        Write-host "`tCustom Script has been Enabled!" -f Green
    }
    Else
    {
        Write-host "`tCustom Script is Already Enabled!" -f Yellow
    }
}

总之,我们展示了如何使用 SharePoint 管理中心和 PowerShell 在 SharePoint Online 中启用自定义脚本。通过执行这些步骤,您可以轻松地为特定网站集或网站或 SharePoint Online 租户中的所有网站集和网站启用自定义脚本。 Microsoft 参考:允许或阻止自定义脚本

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

取消回复欢迎 发表评论:

关灯