簡體   English   中英

如何使用 Powershell CSOM 將用戶添加到 sharepoint 在線站點?

[英]How to add a user to sharepoint online site using Powershell CSOM?

我有以下 Powershell CSOM 代碼將用戶添加到 sharepoint 在線站點。我不是想將他添加到任何組,而是明確授予他訪問該站點的權限。

但是,我收到錯誤:使用“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
}

示例測試腳本供您參考。

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM