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

[玩转系统] 使用 PowerShell 将用户添加到所有 SharePoint Online 网站

作者:精品下载站 日期:2024-12-14 21:32:26 浏览:14 分类:玩电脑

使用 PowerShell 将用户添加到所有 SharePoint Online 网站


要求:使用 PowerShell 将用户添加到 SharePoint Online 中的所有网站。

PowerShell 将用户添加到 SharePoint Online 中的所有网站

您是否曾想向租户的所有 SharePoint Online 网站添加具有读取/编辑/完全控制(或访问者/成员/所有者)权限的用户?将用户添加到 SharePoint 组或向 SharePoint Online 中的用户授予权限非常简单。但要向所有站点授予权限,您必须将用户添加到租户中每个站点上的相应用户组。如果我们使用 PowerShell 自动化此过程不是很好吗?

那么,此 PowerShell 脚本通过将给定用户添加到网站的关联成员组来授予对所有网站的权限:在运行此脚本之前,请确保您拥有所有网站的网站集管理员权限。


#Parameters
$TenantAdminURL="https://crescent-admin.sharepoint.com"
$UserAccount = "[email protected]"

#Get Credentials to Connect
$Cred = Get-Credential

Try {
    #Connect to Tenant Admin
    Connect-PnPOnline -Url $TenantAdminURL -Credentials $Cred

    #Get All Site collections - Exclude: Seach Center, Mysite Host, App Catalog, Content Type Hub, eDiscovery and Bot Sites
    $Sites = Get-PnPTenantSite | Where -Property Template -NotIn ("SRCHCEN#0", "REDIRECTSITE#0","SPSMSITEHOST#0", "APPCATALOG#0", "POINTPUBLISHINGHUB#0", "EDISC#0", "STS#-1")
        
    #Loop through each Site Collection
    ForEach ($Site in $Sites)
    {
        Try {
            #Connect to the Site
            Connect-PnPOnline -Url $Site.Url -Credentials $Cred

            #Get the associated Members Group of the site
            $MembersGroup = Get-PnPGroup -AssociatedMemberGroup
 
            #sharepoint online pnp powershell to add user to group
            Add-PnPGroupMember -LoginName $UserAccount -Identity $MembersGroup
            Write-host "Added User to the site:"$Site.URL -f Green
        }
        Catch {
            write-host -f Red "Error Adding User to the Site: $($Site.URL)" $_.Exception.Message
        }
    }
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

我使用“-AssociatedMemberGroup”开关来获取网站的默认成员组,以授予用户“编辑”权限。如果您想授予用户读取权限,请使用“AssociatedVisitorGroup”,对于“完全控制”或“所有者”权限,请使用:“AssociatedOwnerGroup”作为参数。

授予直接权限级别怎么样?

您可以直接授予权限级别,如下所示:


#Permission Level to Grant
$PermissionLevel = "Contribute"

#grant permission Level to the user
Set-PnPWebPermission -User $UserAccount -AddRole $PermissionLevel

授予所有站点的管理员访问权限怎么样?如何添加对所有 SharePoint Online 网站的管理员访问权限?

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

取消回复欢迎 发表评论:

关灯