【问题标题】:How to add a user to sharepoint online site using Powershell CSOM?如何使用 Powershell CSOM 将用户添加到 sharepoint 在线站点?
【发布时间】:2020-08-10 14:58:40
【问题描述】:

我有以下 Powershell CSOM 代码来将用户添加到共享点在线站点。我不是想将他添加到任何组,而是明确授予他访问该站点的权限。

但是,我收到错误:使用“0”参数调用“ExecuteQuery”的异常:“找不到 ID 为 14 的主体。”谁能告诉我我做错了什么?

Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server 
Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server 
Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

$SiteURL = "https://robinroyrbn.sharepoint.com/sites/AnotherTeamSite"
$UserAccount="i:0#.f|membership|mark@robinroyrbn.onmicrosoft.com"

$PermissionToAdd="Read"


$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

Try {

$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred

$User = $Ctx.Web.SiteUsers.GetByLoginName($UserAccount)

$RoleDefToAdd = $Ctx.web.RoleDefinitions.GetByName($PermissionToAdd)
$RoleAssignment = $Ctx.web.RoleAssignments.GetByPrincipal($User)

$RoleAssignment.RoleDefinitionBindings.Add($RoleDefToAdd)
$RoleAssignment.Update()
$Ctx.ExecuteQuery()

write-host  -f Green "User updated Successfully!"

}
Catch {
write-host -f Red "Error adding User !" $_.Exception.Message
}

【问题讨论】:

    标签: powershell sharepoint-online csom


    【解决方案1】:

    测试脚本示例供您参考。

    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    
    $SiteURL = "https://xxx.sharepoint.com/sites/leetest"
    $UserAccount="user@xxx.onmicrosoft.com"
    
    $PermissionToAdd="Read"
    
    #region Variables 
    $Username = "admin@xxx.onmicrosoft.com" 
    $Password = "password"
    #endregion Variables
    
    #$Cred = Get-Credential
    #$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
    
    Try {
    
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $securePassword=ConvertTo-SecureString $Password -AsPlainText -Force
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $securePassword)
    
    
    $User = $Ctx.Web.EnsureUser($UserAccount)
    $Ctx.Load($User)
    $Ctx.ExecuteQuery()
    
    $RoleDefToAdd = $Ctx.web.RoleDefinitions.GetByName($PermissionToAdd)
    #$RoleAssignment = $Ctx.web.RoleAssignments.GetByPrincipal($User)
    #$RoleAssignment.RoleDefinitionBindings.Add($RoleDefToAdd)
    #$RoleAssignment.Update()
    $collRdb = new-object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($Ctx)
    $collRdb.Add($RoleDefToAdd)
    $collRoleAssign = $Ctx.Web.RoleAssignments
    $rollAssign = $collRoleAssign.Add($User, $collRdb)
    $Ctx.ExecuteQuery()
    
    write-host  -f Green "User updated Successfully!"
    
    }
    Catch {
    write-host -f Red "Error adding User !" $_.Exception.Message
    }
    

    【讨论】:

    • 是的。它工作正常。谢谢@Lee_MSFT 的回答。
    猜你喜欢
    • 1970-01-01
    • 2023-02-16
    • 2017-11-23
    • 1970-01-01
    • 2011-03-28
    • 2023-03-09
    • 2017-04-04
    • 2014-03-13
    • 2017-09-16
    相关资源
    最近更新 更多