SharePoint Online:使用 PowerShell 克隆用户组成员身份
要求:将用户的组权限克隆到 SharePoint Online 网站中的另一个用户。
PowerShell 在 SharePoint Online 中克隆用户的组成员身份:
这是一个常见的现实场景,当有人离开组织,而其他人接手该角色时,他们想要相同的权限!虽然这个要求听起来很简单,但通过 Web 用户界面来实现却很麻烦。基本上,您必须转到源用户所属的每个组并将目标用户添加到其中。我们可以使用 PowerShell 自动执行此任务,将用户组成员身份从一个帐户克隆到另一个帐户!
#Function to copy group memberships from one user to another
Function Clone-SPOUserGroupPermission($SiteURL, $SourceUserID, $TargetUserID)
{
Try {
#Get the Site collection
$Site = Get-SPOSite $SiteURL
#Get the Source user
$SourceUser = Get-SPOUser -Site $Site -LoginName $SourceUserID
#Get All groups of the source User
$UserGroups = $SourceUser.Groups
If($UserGroups.Count -gt 0)
{
Write-host -f Yellow "Found the User Member of $($UserGroups.Count) Group(s)..."
#Loop through each group of the source user and add target user to that group
ForEach ($Group in $UserGroups)
{
#Add Target User to the Group
Add-SPOUser -Site $Site -LoginName $TargetUserID -Group $Group | out-null
Write-Host -f Green "Added $TargetUserID to Group '$Group'"
}
}
}
Catch {
Write-host $_.Exception.Message -f Red
}
}
#SharePoint Online Admin Center URL
$AdminCenterURL = "https://crescent-admin.sharepoint.com"
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$SourceUserID ="John@Crescent.com"
$TargetUserID ="Peter@Crescent.com"
#Connect to SharePoint Online Admin Center
Connect-SPOService -Url $AdminCenterURL -Credential (Get-Credential)
#Call the Function
Clone-SPOUserGroupPermission -SiteURL $SiteURL -SourceUserID $SourceUserID -TargetUserID $TargetUserID
您还可以使用“Get-SPOSite”cmdlet 获取所有网站集并调用该函数将用户的组成员身份复制给另一个用户。
PnP PowerShell 将用户的组权限复制给另一个用户
以下是如何使用 PnP PowerShell 在 SharePoint Online 中克隆用户成员身份:
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Retail"
$SourceUserLoginID = "steve@crescent.com"
$TargetUserLoginID = "alexw@crescent.com"
Try {
#Connect to the Source Site
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the source User
$User = Get-PnPUser -Identity "i:0#.f|membership|$UserLoginID"
#Get all groups of the user
$UserGroups = $User.Groups | Where {$_.LoginName -notlike "*Limited Access*"}
#Add Target User to the Groups
$UserGroups | ForEach-Object {
Add-PnPGroupMember -LoginName $TargetUserLoginID -Identity $_.LoginName
Write-host -f Green "Added User to the Group:"$_.LoginName
}
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
如果您想将权限从一个用户完全复制到另一个用户,而不仅仅是复制组权限,该怎么办?因为源用户可能还拥有其他级别的权限。例如,作为网站集管理员,可以在列表/文件夹/项目级别直接授予权限(例如“完全控制”)!因此,这里是克隆用户权限的 PowerShell:如何使用 PowerShell 在 SharePoint Online 中复制用户权限?
