我正在尝试将名为“Students”的特定组的只读权限添加到我创建的名为“Quiz”的列表中。我必须使用PowerShell CSOM,但在其他所有教程中,我已经使用了.NET服务器类型,这些类型不适用于我的代码。使用Powershell CSOM为Sharepoint组添加只读权限
代码:
$ListName = "Quiz"
$PermissionLevel = "Read Only"
$web = $ctx.Web
$lists = $web.Lists
$ctx.Load($lists)
$ctx.ExecuteQuery()
foreach($list in $lists)
{
if($list.Title -eq $ListName)
{
$listId = $list.Id
}
}
$list = $lists.GetById($listId)
$ctx.Load($list);
$ctx.ExecuteQuery();
Write-Host "List:" $List.Title -foregroundcolor Green
if ($list -ne $null)
{
$groups = $web.SiteGroups
$ctx.Load($groups)
$ctx.ExecuteQuery()
foreach ($SiteGroup in $groups) {
if ($SiteGroup.Title -match "Students")
{
write-host "Group:" $SiteGroup.Title -foregroundcolor Green
$GroupName = $SiteGroup.Title
$builtInRole = $ctx.Web.RoleDefinitions.GetByName($PermissionLevel)
$roleAssignment = new-object Microsoft.SharePoint.Client.RoleAssignment($SiteGroup)
$roleAssignment.Add($builtInRole)
$list.BreakRoleInheritance($True, $False)
$list.RoleAssignments.Add($roleAssignment)
$list.Update();
Write-Host "Successfully added <$GroupName> to the <$ListName> list in <$site>. " -foregroundcolor Green
}
else
{
Write-Host "No Students groups exist." -foregroundcolor Red
}
}
}
我的错误是在
$roleAssignment = new-object Microsoft.SharePoint.Client.RoleAssignment($SiteGroup)
,在那里我recieving错误
Cannot find an overload for "RoleAssignment" and the argument count: "1".
大多数教程使用
$roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($SiteGroup)
我无法使用。
我该如何填写我的代码?
P.S.我知道我的代码有点乱,但是我花了太多时间来寻找解决方案,并且过去几个小时我的代码质量大大降低。对不起。