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

[玩转系统] 如何使用 PowerShell 将 SharePoint 列表或库设置为只读模式?

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

如何使用 PowerShell 将 SharePoint 列表或库设置为只读模式?


要求:将 SharePoint 列表或文档库设置为只读模式。

如何将 SharePoint 列表设置为只读?

没有直接的方法将 SharePoint 列表设置为只读!只能在 SharePoint 网站集(如何将网站集设为只读)或内容数据库(将 SharePoint 内容数据库设置为只读模式)上设置只读模式。但是,我们可以通过将所有用户的权限替换为“只读”来将 SharePoint 列表设置为只读。

[玩转系统] 如何使用 PowerShell 将 SharePoint 列表或库设置为只读模式?

这些方法不能控制场管理员、网站集管理员!

如何从 SharePoint 用户界面将 SharePoint 列表或库设置为只读:

如果您曾经需要将 SharePoint 列表或库设置为只读,您可能会发现没有直接的方法可以使用 UI 来执行此操作。但是我们可以通过替换 SharePoint 列表的所有权限来将其设置为只读模式。

  1. 转到列表设置>>此列表的权限
  2. 单击功能区中的“停止继承权限”按钮并确认提示
  3. 选择所有用户 >> 单击“编辑用户权限”按钮。这将导致“编辑权限”页面。 从选择权限部分选择“读取”,然后单击“确定”。
  4. 现在,您会看到所有用户权限都更改为“读取”。

    [玩转系统] 如何使用 PowerShell 将 SharePoint 列表或库设置为只读模式?

同样的方法可以使文档库在 SharePoint 中变为只读。

使用 PowerShell 将 SharePoint 列表更改为只读模式:

让我们使用 PowerShell 将 SharePoint 列表更改为只读。


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Parameters
$SiteURL="https://intranet.crescent.com/"
$ListName= "Projects"

#Get the Web and List
$Web = Get-SPWeb $SiteURL
$List = $Web.Lists.TryGetList($ListName)

#Break Permissions of the List
If ($List.HasUniqueRoleAssignments -eq $false)
{
    $List.BreakRoleInheritance($true)
}

#Get Read Permission Level
$ReadPermission = $web.RoleDefinitions["Read"]

#Get All User & Groups granted Permissions to the List 
ForEach ($RoleAssignment in $List.RoleAssignments) 
{
    Write-host "Resetting Permissions for :"$RoleAssignment.Member.Name

    #Remove All permissions
    $RoleAssignment.RoleDefinitionBindings.RemoveAll()
    $RoleAssignment.RoleDefinitionBindings.Add($ReadPermission)
    $RoleAssignment.Update()
}

这会将列表设置为只读。但是等等,有一个问题!上述 UI 方法或 PowerShell 脚本将所有权限重置为只读,无论安全主体(例如用户、组等)是否已经具有“仅查看”或“受限访问”等权限,都没有关系。因此,让我们稍微调整一下以替换除“读取”、“仅查看”、“受限读取”和“有限访问”之外的权限。

有限访问是一种特殊的权限级别,当用户被分配对权限继承已损坏的子对象的权限时,会自动授予该权限级别。例如。当您授予列表项权限时,如果用户还没有访问列表的权限,SharePoint 会自动授予列表级别的“有限访问权限”!

PowerShell 使 SharePoint 库只读:

如果您想限制用户对列表或库进行更改,这会很有用。


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Parameters
$SiteURL="https://intranet.crescent.com/"
$ListName= "Projects"

#Get the Web and List
$Web = Get-SPWeb $SiteURL
$List = $Web.Lists.TryGetList($ListName)

#Break Permissions of the List
If ($List.HasUniqueRoleAssignments -eq $false)
{
    $List.BreakRoleInheritance($true)
}

#Get Read Permission Level
$ReadPermission = $web.RoleDefinitions["Read"]

#Get All User & Groups granted Permissions to the List 
ForEach ($RoleAssignment in $List.RoleAssignments) 
{
    Write-host "Resetting Permissions for :"$RoleAssignment.Member.Name -f Yellow

    #Replace All other permissions with "Read" if its not granted already
    $RoleDefinitionBindings = $RoleAssignment.RoleDefinitionBindings
    Foreach($RoleDefBinding in $RoleDefinitionBindings)
    {
        IF( ($RoleDefBinding.Name -ne "Read") -and ($RoleDefBinding.Name -ne "Restricted Read") -and ($RoleDefBinding.Name -ne "View Only") -and ($RoleDefBinding.Name -ne "Limited Access") )
        {
            #Grant Read ACcess if its not present
            If(!($RoleAssignment.RoleDefinitionBindings.Contains($ReadPermission)))
            {
                $RoleAssignment.RoleDefinitionBindings.Add($ReadPermission)
                $RoleAssignment.Update()
                Write-host "Added Read Permissions to "$RoleAssignment.Member.Name -ForegroundColor Green
            }
        }
        else 
        { 
            continue;
        }
    }

    #Remove All permissions other than Read or Similar
    $RoleDefinitionBindings = $RoleAssignment.RoleDefinitionBindings
    For($i=$RoleDefinitionBindings.Count-1; $i -ge 0; $i--)
    {
        $RoleDefBinding = $RoleAssignment.RoleDefinitionBindings[$i]        

        IF( ($RoleDefBinding.Name -eq "Read") -or ($RoleDefBinding.Name -eq "Restricted Read") -or ($RoleDefBinding.Name -eq "View Only") -or ($RoleDefBinding.Name -eq "Limited Access") )
        {
            continue;
        }
        Else
        {
            $RoleAssignment.RoleDefinitionBindings.Remove($RoleAssignment.RoleDefinitionBindings[$i])
            $RoleAssignment.Update()
            Write-host  Removed  $RoleDefBinding.Name Permissions from $RoleAssignment.Member.Name -ForegroundColor Red
        }
    }
}

备份列表的当前权限,然后在需要时恢复权限,这不是一个好主意吗?

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

取消回复欢迎 发表评论:

关灯