如何使用 Powershell CSOM 将用户添加到 Sharepoint 在线站点?
How to add a user to sharepoint online site using Powershell CSOM?
我有以下 Powershell CSOM 代码可以将用户添加到在线共享点 site.I 我不是要将他添加到任何组,而是要明确授予他访问该站点的权限。
但是,我收到错误:使用“0”参数调用 "ExecuteQuery" 异常:"Can not find the principal with id: 14."谁能告诉我我做错了什么?
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server
Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server
Extensions\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\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\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
}
我有以下 Powershell CSOM 代码可以将用户添加到在线共享点 site.I 我不是要将他添加到任何组,而是要明确授予他访问该站点的权限。
但是,我收到错误:使用“0”参数调用 "ExecuteQuery" 异常:"Can not find the principal with id: 14."谁能告诉我我做错了什么?
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server
Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server
Extensions\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\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\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
}