当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 从组中删除用户

作者:精品下载站 日期:2024-12-14 14:09:37 浏览:13 分类:玩电脑

SharePoint Online:使用 PowerShell 从组中删除用户


要求:使用 PowerShell 从 SharePoint Online 中的组中删除用户。

作为管理员,有时您可能需要从 SharePoint Online 中的组中删除用户。在本文中,我们将使用 Web 浏览器界面和 PowerShell 逐步完成执行此操作所需的步骤。

SharePoint Online:如何从组中删除用户?

只需单击几下即可从 SharePoint Online 安全组中删除用户。请按照以下步骤从 SharePoint Online 组中删除用户:

  1. 导航到使用该组的 SharePoint Online 网站,单击“网站设置”齿轮 >> 从菜单中单击“网站设置”。
  2. 在“站点设置”页面中,单击“用户和权限”部分下的“人员和组”。
  3. 在左侧导航菜单中,选择您要从中删除用户的组。
  4. 勾选您要删除的用户旁边的复选框。
  5. 单击操作下拉箭头 >> 选择从组中删除用户。
  6. 在弹出的确认消息框中,单击“确定”确认删除。

    [玩转系统] SharePoint Online:使用 PowerShell 从组中删除用户

如果您没有看到“操作”菜单,那是因为您无权编辑该组的成员!

SharePoint Online:使用 PowerShell 从组中删除用户

如果您需要从 SharePoint Online 组中删除用户,则无需浏览“用户和组”页面。相反,您可以使用 PowerShell 快速轻松地从组中删除用户。让我们使用 PowerShell 从 SharePoint Online 组中删除用户:


#Load SharePoint CSOM Assemblies
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"

#sharepoint online remove user from site collection powershell
Function Remove-UserFromGroup()
{
  param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $GroupName,
        [Parameter(Mandatory=$true)] [string] $UserID
    )
   Try {
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
        
        #Get the User
        $User=$Ctx.web.EnsureUser($UserID)
        $Ctx.Load($User)
        #Get the Group
        $Group=$Ctx.web.SiteGroups.GetByName($GroupName)
        $Ctx.Load($Group)
        $Ctx.ExecuteQuery()

        #Check if user member of the group
        $IsMember = $False
        $GroupUsers = $Group.Users 
        $Ctx.Load($GroupUsers)
        $Ctx.ExecuteQuery()
        Foreach($GrpUser in $GroupUsers){
            if($GrpUser.id -eq $User.Id)
            {
                $IsMember = $True
            }
        }
        if($IsMember -eq $False)
        {
            Write-host "User Doesn't Exists in the Group!" -ForegroundColor Yellow
        }
        else
        {
            #Remove user from the group
            $Group.Users.RemoveByLoginName($User.LoginName)
            $Ctx.ExecuteQuery()
            Write-host "User Removed from the Group Successfully!" -ForegroundColor Green
        }
    }
    Catch {
        write-host -f Red "Error Removing User from Group!" $_.Exception.Message
    }
} 

#Set parameter values
$SiteURL = "https://crescent.sharepoint.com"
$GroupName="Team Site Members"
$UserID="[email protected]"

#Call the function to remove user from group
Remove-UserFromGroup -SiteURL $SiteURL -GroupName $GroupName -UserID $UserID 

使用 PowerShell 从 SharePoint Online 组中删除所有用户

从组中删除所有用户怎么样?让我们演练一下使用 PowerShell 从 SharePoint Online 中的组中删除用户的过程:


#Load SharePoint Online Assemblies
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"

#Function to remove all users from a group
Function Remove-AllUserFromGroup()
{
  param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $GroupName
    )
   Try {
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
         
        #Get the Group
        $Group=$Ctx.web.SiteGroups.GetByName($GroupName)
        $Ctx.Load($Group)
        $Ctx.ExecuteQuery()
 
        #Get users of the group
        $GroupUsers = $Group.Users
        $Ctx.Load($GroupUsers)
        $Ctx.ExecuteQuery()
 
        #Remove all users from the group
        ForEach($User in $GroupUsers)
        {
            $Group.Users.RemoveByLoginName($User.LoginName)
        }
        $Ctx.ExecuteQuery()
        Write-host "All Users are Removed from the Group!" -ForegroundColor Green        
    }
    Catch {
        write-host -f Red "Error Removing All Users from Group!" $_.Exception.Message
    }
} 

#Set parameter values
$SiteURL = "https://crescent.sharepoint.com"
$GroupName="Team Site Members"

#Call the function to remove all users from group
Remove-AllUserFromGroup -SiteURL $SiteURL -GroupName $GroupName

使用 SharePoint Online Management Shell 从组中删除用户

我们还可以使用Remove-SPOUser cmdlet 从特定组中删除用户。


#Set parameter values
$AdminSiteURL="https://crescent-admin.sharepoint.com"
$SiteURL="https://crescent.sharepoint.com"
$GroupName="Team Site Members"
$LoginName="[email protected]"

#Get Credentials to connect
$Credentials = Get-Credential
Connect-SPOService -url $AdminSiteURL -credential $Credential

#remove user from sharepoint online group powershell
Remove-SPOUser -LoginName $LoginName -Site $SiteURL -Group $GroupName

[玩转系统] SharePoint Online:使用 PowerShell 从组中删除用户

从网站的所有组中删除用户

从网站的所有组中删除用户怎么样?下面介绍了如何使用 PowerShell 从 SharePoint Online 中的组中删除用户。首先,我们将使用 PowerShell 登录到 SharePoint Online 网站。接下来,我们将获取用户所属组的列表。最后,我们将从所需的组中删除用户。让我们开始吧!


#Set parameter values
$AdminSiteURL="https://crescent-admin.sharepoint.com"
$SiteURL="https://crescent.sharepoint.com"
$LoginName="[email protected]"

#Get Credentials to connect
$Credentials = Get-Credential
Connect-SPOService -url $AdminSiteURL -credential $Credential

#Get all groups  
$Groups= Get-SPOSiteGroup -Site $SiteURL

#Remove user from each group 
Foreach($Group in $Groups)
{
   Remove-SPOUser -Site $SiteURL -LoginName $LoginName -Group $Group.Name
}

其他从所有组中删除用户的方法(例如 CSOM 和 PnP PowerShell)如下:如何使用 PowerShell 从 SharePoint Online 站点中的所有组中删除用户?

SharePoint Online:使用 PnP PowerShell 从组中删除用户

让我们使用 PnP PowerShell 从 SharePoint Online 组中删除用户:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$GroupName ="Marketing Members"
$UserLoginId = "[email protected]"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
    
#Remove user from group
Remove-PnPGroupMember -LoginName $UserLoginId -Identity $GroupName

如果您正在管理 SharePoint Online,有时可能需要从组中删除多个用户。您可以通过将多个用户包装到一个数组中来从组中删除它们!


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Retail/"
$UserIds = @("[email protected]","[email protected]","[email protected]")

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get the Members Group of the site
$MembersGroup = Get-PnPGroup -AssociatedMemberGroup

#Get All members of the Group
$GroupMembers = Get-PnPGroupMember -Identity $MembersGroup | Select -ExpandProperty "Email"
ForEach($UserID in $UserIds)
{
    #Check if the user is member of the group
    If($GroupMembers.Contains($UserID))
    {
        #Remove user from group
        Remove-PnPGroupMember -LoginName $UserId -Identity $MembersGroup
        Write-Host -f Green "User '$UserID' Removed from the Group Successfully!"
    }
    Else
    {
        Write-Host -f Yellow "User '$UserID' is not part a member of the Group!"
    }
}

如果用户被授予对站点的直接访问权限(例如完全控制、编辑、读取等),则从组中删除用户可能不会阻止用户访问站点。这是另一篇关于从网站中完全删除用户的文章:如何使用 PowerShell 从 SharePoint Online 网站中删除用户?

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯