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

[玩转系统] SharePoint Online:使用 PowerShell 获取网站集管理员

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

SharePoint Online:使用 PowerShell 获取网站集管理员


要求: SharePoint Online PowerShell 列出所有网站集管理员。

SharePoint Online 网站集管理员拥有对整个网站集的完全控制权,包括顶级网站、子网站、网站集中的所有列表和库。每个 SharePoint 网站集都可以有自己的网站集管理员,通常在 SharePoint Online 管理员通过 SharePoint Online 管理中心网站创建网站集期间分配,作为主要网站所有者和网站集管理员。可以通过网站设置页面添加/删除任何其他网站集管理员。

如何从 SharePoint 管理中心获取所有网站集管理员?

如何在 SharePoint Online 中找到集合管理员?要获取所有网站集管理员,请从您最喜欢的浏览器(例如 Microsoft Edge 或 Google Chrome)登录 SharePoint 管理中心 >> 展开“站点”>> 活动站点 >> 从列表中选择站点 >> 单击“权限”,然后单击选择“管理管理员”。

[玩转系统] SharePoint Online:使用 PowerShell 获取网站集管理员

网站集管理员与主要 SharePoint 网站所有者具有相同的访问权限,但唯一的区别是电子邮件通知!主要网站集管理员将收到电子邮件通知,例如网站集存储限制警告电子邮件。

如何从网站设置中获取 SharePoint Online 中的网站集管理员?

如果您想要获取 SharePoint Online 网站的所有网站集管理员,请执行以下操作:

  1. 单击设置齿轮图标并选择站点设置。
  2. 在“网站设置”页面上,单击“用户和权限”组下的“网站集管理员”链接(在浏览器中点击此 URL 快捷方式:/_layouts/15/mngsiteadmin.aspx)。
  3. 此页面为您提供特定网站集的网站集管理员列表。

    [玩转系统] SharePoint Online:使用 PowerShell 获取网站集管理员

在现代组连接网站上,网站集管理员位于“设置”>>“网站权限”>>“高级权限设置”下。

我们在 SharePoint Online 中没有辅助网站集管理员。网站集可以有一个主要网站所有者和其他网站集管理员!

现在,让我们使用 PowerShell 脚本来获取 SharePoint Online 中的网站集管理员。

使用 PowerShell 获取主网站集管理员 - 所有者

以下是如何使用 PowerShell 脚本获取 SharePoint Online 中的网站集管理员:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll"
  
#Get Primary Owners of All Site collections from the Tenant
Function Get-SPOPrimarySiteOwners($AdminSiteURL, $Cred)
{
    #Setup credentials to connect
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
   
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminSiteURL)
    $Ctx.Credentials = $Credentials
  
    #Get the tenant object 
    $Tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($ctx)
  
    #Get All Site Collections
    $SiteCollections=$Tenant.GetSitePropertiesFromSharePoint(0,$true)
    $Ctx.Load($SiteCollections)
    $Ctx.ExecuteQuery()
 
    #Iterate through Each site collection
    ForEach($Site in $SiteCollections)
    {
        #Get the Site Collection and Audit Objects
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($Site.URL)
        $Ctx.Credentials = $Credentials
        
        #Get the Site collection
        $SiteCollection = $Ctx.Site
        $Ctx.Load($SiteCollection)
        $Ctx.ExecuteQuery()
 
        #Get Site Owner
        $Owner = $SiteCollection.Owner
        $Ctx.Load($Owner)
        $Ctx.ExecuteQuery()
        
        #Output Primary Site owner Details
        $SiteCollection | Select URL, @{Name="Owner Name";Expression={$_.Owner.Title}}, @{Name="Owner Email";Expression={$_.Owner.Email}}
    }
}
  
#Set Parameters
$AdminSiteUrl = "https://Crescent-admin.sharepoint.com/"
$Cred= Get-Credential
 
Get-SPOPrimarySiteOwners -AdminSiteURL $AdminSiteUrl -Cred $Cred

在执行此脚本之前,请确保您的本地计算机中已安装 CSOM SDK 或 SharePoint Online PowerShell 模块。您可以使用 Windows PowerShell ISE 运行该脚本。它使用 SharePoint Online 中的 PowerShell 获取主要网站集管理员。

SharePoint Online:使用 PowerShell 获取网站集管理员

您可以使用 PowerShell cmdlet Get-SPOUser 以及筛选器 IsSiteAdmin 属性来获取给定网站集的网站集管理员列表。这使您既成为主要网站所有者又成为网站集管理员。


#Variables for processing
$AdminURL = "https://Crescent-admin.sharepoint.com/"
$AdminName = "[email protected]"
$SiteCollURL="https://Crescent.sharepoint.com/sites/sales"
 
#User Names Password to connect 
$Password = Read-host -assecurestring "Enter Password for $AdminName" 
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Password

#Connect to SharePoint Online
Connect-SPOService -url $AdminURL -credential $Credential

#Get the Site colection
$SiteColl = Get-SPOSite $SiteCollURL
    
#sharepoint online powershell get all site collection admins
$SiteAdmins = Get-SPOUser -Site $SiteCollURL -Limit ALL | Where { $_.IsSiteAdmin -eq $True}
foreach($Admin in $SiteAdmins)
{
    Write-host $Admin.LoginName        
}

PowerShell 获取 SharePoint Online 中所有网站集的网站集管理员

让我们添加一些错误处理并从所有网站集中获取网站集管理员。


#Variables for processing
$AdminURL = "https://Crescent-admin.sharepoint.com"
$AdminName = "[email protected]"
 
#User Names Password to connect 
$Password = Read-host -assecurestring "Enter Password for $AdminName" 
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Password

Try {
    #Connect to SharePoint Online
    Connect-SPOService -url $AdminURL -credential $Credential

    #Get all Site colections
    $Sites = Get-SPOSite -Limit ALL

    Foreach ($Site in $Sites)
    {
        Write-host $Site.URL
    
        #Get all Site Collection Administrators
        $SiteAdmins = Get-SPOUser -Site $Site.Url -Limit ALL | Where { $_.IsSiteAdmin -eq $True}
        foreach($Admin in $SiteAdmins)
        {
            Write-host $Admin.LoginName        
        }
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

在 SharePoint Online 中使用 PnP PowerShell 获取所有网站集管理员

借助 PnP PowerShell,可以使用 Get-PnPSiteCollectionAdmin cmdlet 检索网站集管理员。以下是用于获取所有网站集管理员的 SharePoint Online PowerShell:

您必须具有网站集管理员权限,才能通过 PowerShell 获取 SharePoint Online 网站的现有网站集管理员!您可能会收到错误“Get-PnPSiteCollectionAdmin:远程服务器返回错误:(403) 禁止。”如果你不是!

#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get Site Collection Administrators
Get-PnPSiteCollectionAdmin

此 PnP PowerShell 脚本获取给定网站集的所有网站集管理员。要获取租户中所有网站的网站集管理员,请使用以下命令:


#Set Variables
$SiteURL = "https://crescent.sharepoint.com"

#Get Credentials to connect
$Cred = Get-Credential

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials $Cred

#Get All Site collections  
$SitesCollection = Get-PnPTenantSite

#Loop through each site collection
ForEach($Site in $SitesCollection)  
{  
    Write-host -F Green "Site Collection Administrators of site: " $Site.Url  
    Connect-PnPOnline -Url $Site.Url -Credentials $Cred
  
    #Get Site Collection administrators of the site
    Get-PnPSiteCollectionAdmin
}

从组连接站点获取站点集管理员

如果站点是组连接的,您将在上面的脚本输出中仅看到“组所有者”。以下是如何扩展群组并获得群组所有者的方法:


#Set Variables
$AdminSiteURL = "https://crescent-admin.sharepoint.com"
 
#Connect to Admin Center
Connect-PnPOnline -Url $AdminSiteURL -Interactive
  
#Get All Site collections - Exclude: Seach Center, Mysite Host, App Catalog, Redirect, Content Type Hub, eDiscovery and Bot Sites
$SiteCollections = 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 $SiteCollections) 
{ 
    Write-host -F Yellow "Getting Site Collection Administrators of the site: " $Site.Url 
    Connect-PnPOnline -Url $Site.Url -Interactive
    
    #Get Site Collection Administrators
    $SiteAdminstrators = @()
    Get-PnPSiteCollectionAdmin -PipelineVariable Admin | ForEach-Object {
        If($_.PrincipalType -eq "SecurityGroup")
        {
            #Get Members of the Group
            $Group = Get-PnPMicrosoft365Group -IncludeOwners | Where {$_.Mail -eq $Admin.Email}
            $Group.Owners | Select Email | ForEach-Object {
                $SiteAdminstrators += $_.Email
            }
        }
        Else
        {
            $SiteAdminstrators += $Admin.Email
        }
    }
    #Get Site Collection Administrators of the Site
    $SiteAdminstrators | select -Unique
}

如果需要将所有网站集管理员导出到 CSV 文件,请参阅:将 SharePoint Online 网站集管理员导出到 CSV

经常问的问题:

如何使用 PowerShell 在 SharePoint Online 中添加网站集管理?

要添加 SharePoint 网站集管理员,请使用:例如,SharePoint Online Management Shell 中的“Set-SPOUser -site $SiteURL -LoginName 'UPN of the user' -IsSiteCollectionAdmin $True”或使用 PnP PowerShell 的 Add-PnPSiteCollectionAdmin/Set-PnPTenantSite cmdlet 。您还可以使用客户端对象模型设置用户的“IsSiteAdmin”属性来设置网站集管理员。
详细信息:SharePoint Online PowerShell 添加网站集管理员

如何删除 SharePoint Online 中的集合管理员?

您可以使用 SharePoint Online 管理中心、网站设置页面或 PowerShell 脚本从 SharePoint Online 网站中删除网站集管理员。
详细信息:在 SharePoint Online 中删除网站集管理员

如何更改 SharePoint Online 中的主管理员?

您可以通过 SharePoint 管理中心更改网站所有者,方法是选择网站集,单击“权限”,然后单击“管理管理员”。在“管理管理员”面板上,您可以通过将网站集主管理员设置为“主管理员”来更改网站集所有者。
更多信息:如何在 SharePoint Online 中更改网站集所有者(主管理员)?

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

取消回复欢迎 发表评论:

关灯