powershell 正在获取所有SharePoint网站的所有者

z9smfwbn  于 2023-08-05  发布在  Shell
关注(0)|答案(3)|浏览(134)

我需要得到所有SharePoint网站的所有者(包括经典,现代和SharePoint网站连接到团队)。大约有1000个站点your text
我已经运行了以下,但它只给我有限的业主,而不是所有的业主和一些网站只列出网站管理员作为业主,而不是实际的网站所有者。我知道并已通过SharePoint管理中心验证,每个网站都有所有者/网站所有者组中的成员。
`` $AdminCenterURL =“https://XXX-admin.sharepoint.com”;$CSVPath =“C:\Temp\SiteOwners.csv”

#Connect to SharePoint Online and Azure AD
Connect-SPOService -url $AdminCenterURL
Connect-AzureAD

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

 $SiteOwners = @()
 #Get Site Owners for each site collection
 $Sites | ForEach-Object {
 If($.Template -like ‘GROUP*’)
 {
 $Site = Get-SPOSite -Identity $.URL
 #Get Group Owners
 $GroupOwners = (Get-AzureADGroupOwner -ObjectId $Site.GroupID | Select -ExpandProperty UserPrincipalName) -     join "; "
 }
 Else
 {
 $GroupOwners = $.Owner
 }
 #Collect Data
 $SiteOwners += New-Object PSObject -Property @{
‘Site Title’ = $.Title
‘URL’ = $_.Url
‘Owner(s)’ = $GroupOwners
}
}
#Get Site Owners
$SiteOwners

#Export Site Owners report to CSV
$SiteOwners | Export-Csv -path $CSVPath -NoTypeInformation`

字符串
`
谁能帮我,我哪里做错了,或者让我知道是否有更好的方法?我需要得到的所有租赁SharePoint网站的所有业主,有超过1000个租赁网站
先谢了

ioekq8ef

ioekq8ef1#

看来你的代码中的大多数问题都来自于从互联网上复制/粘贴。首先,GROUP*周围的单引号是特殊的花引号。其次,在$_.xyz引用中缺少了几个_。最后是一些坏习惯,比如先创建一个数组$blah = @(),然后再创建一个+=,这只是创建了原始数组的一个副本。如果这不是一个很大的列表,这不是一个大问题,但绝对没有理由。另一个坏习惯是创建自定义对象的老派做法。我已经重写了代码,并在我这边测试过了,看起来不错。

$AdminCenterURL = “https://XXX-admin.sharepoint.com”
$CSVPath = “C:\Temp\SiteOwners.csv”

Connect-SPOService -url $AdminCenterURL
Connect-AzureAD

$sitelist = Get-SPOSite -Limit ALL

$siteownerlist = foreach($site in $sitelist){
    $ownerlist = if($site.Template -like '*group*'){
        Get-AzureADGroupOwner -ObjectId $site.GroupId | Select-Object -ExpandProperty UserPrincipalName
    }
    else{
        $site.Owner
    }

    [PSCustomObject]@{
        'Site Title' = $site.Title
        URL          = $site.Url
        'Owner(s)'   = $ownerlist -join '; '
    }
}

$siteownerlist | Export-Csv -path $CSVPath -NoTypeInformation`

字符串
如果您计划长期使用此命令,建议您将AzureAD命令替换为Microsoft图形命令。

编辑

也许sharepoint online/exchange online方法可以提供您正在寻找的数据。

$adminurl = 'https://yourtenant-admin.sharepoint.com'

Connect-SPOService -Url $adminurl
Connect-ExchangeOnline

Function Get-GroupOwner{
    [cmdletbinding()]
    Param(
        [string]$SiteURL
    )

    $Owners = $SiteOwners = $Null

    $sposite = Get-SPOSite -Identity $SiteURL

    $Groupid = if($sposite.Template -match  "TEAMCHANNEL#\d"){
        $sposite.GroupId
    }
    else{
        $sposite.RelatedGroupId
    }

    $Owners = if($GroupId.Guid -eq "00000000-0000-0000-0000-000000000000"){
        "Deleted group"
    }
    else{      
        try{ 
            Get-UnifiedGroupLinks -Identity $GroupId.Guid -LinkType Owners -ErrorAction Stop
        }
        catch{
            "Possibly deleted Office 365 Group"
        }
    }

    $SiteOwners = if($Null -eq $Owners){
        "No owner located"
    }
    elseif($Owners -isnot [string]){
        ($Owners | ForEach-Object {
            "{0} ({1})" -f $_.DisplayName,$_.PrimarySmtpAddress
        }) -join '; '
    }

    [PSCustomObject]@{
        Site   = $siteurl
        Owners = $SiteOwners
    }
}

# Check that we are connected to Exchange Online and SharePoint Online
$ModulesLoaded = Get-Module | Select-Object Name

If (!($ModulesLoaded -match "ExchangeOnlineManagement")) {
    Write-Host "Please connect to the Exchange Online Management module and then restart the script" -ForegroundColor Yellow
    break
}

If (!($ModulesLoaded -match "Microsoft.Online.Sharepoint.PowerShell")) {
    Write-Host "Please connect to the SharePoint Online Management module and then restart the script" -ForegroundColor Yellow
    break
}

$sites = Get-SPOSite -Limit All |
    Select-Object Title, URL, StorageQuota, StorageUsageCurrent, Template |
        Sort-Object StorageUsageCurrent -Descending

$totalspostorageused = [Math]::Round(($sites.StorageUsageCurrent | Measure-Object -Sum).Sum /1024,2)

$report = foreach($site in $sites){
    $siteowners = $Null

    Write-Host Processing site: $site.Title -ForegroundColor Cyan

    $sitetype,$siteowners = switch -Regex ($Site.Template) {
        "RedirectSite#\d"            {"Redirect",(Get-GroupOwner $site.url).Owners}
        "GROUP#\d"                   {"Group-enabled team site",(Get-GroupOwner $site.url).Owners}
        "TEAMCHANNEL#\d"             {"Teams Private Channel",(Get-GroupOwner $site.url).Owners}
        "REVIEWCTR#\d"               {"Review Center",(Get-GroupOwner $site.url).Owners}
        "APPCATALOG#\d"              {"App Catalog",(Get-GroupOwner $site.url).Owners}
        "STS#\d"                     {"Team Site","System"}
        "SPSMSITEHOST#\d"            {"Unknown",(Get-GroupOwner $site.url).Owners}
        "SRCHCEN#\d"                 {"Search Center",(Get-GroupOwner $site.url).Owners}
        "EHS#\d"                     {"Team Site - SPO Configuration","System"}
        "EDISC#\d"                   {"eDiscovery Center",(Get-GroupOwner $site.url).Owners}
        "SITEPAGEPUBLISHING#\d"      {"Site page", "System"}
        "POINTPUBLISHINGHUB#\d"      {"Communications Site","System" }
        "POINTPUBLISHINGPERSONAL#\d" {"OneDrive for Business",(Get-GroupOwner $site.url).Owners}
        "POINTPUBLISHINGTOPIC#\d"    {"Office 365 Video","System"}
    }

    $usedGB = [Math]::Round($site.StorageUsageCurrent/1024,2) 
    $percenttenant = ([Math]::Round($site.StorageUsageCurrent/1024,4)/$totalspostorageused).tostring("P")           

    [PSCustomObject]@{
        URL           = $site.URL
        SiteName      = $site.Title
        Owner         = $siteowners
        Template      = $sitetype
        QuotaGB       = [Math]::Round($site.StorageQuota/1024,0) 
        UsedGB        = $usedGB
        PercentUsed   = ([Math]::Round(($site.StorageUsageCurrent/$site.StorageQuota),4).ToString("P")) 
        PercentTenant = $percenttenant
    }
}

$report

b4wnujal

b4wnujal2#

只是为了澄清-目前只有1名成员的“网站管理员”正在为每个网站列出,而不是所有的“网站成员”组的成员,这是我所需要的。有以下组的经典SharePoint网站-网站管理员,网站所有者,网站成员和网站访问者-这是网站所有者成员,我感兴趣的。
x1c 0d1x的数据

b5lpy0ml

b5lpy0ml3#

请参阅本文以使用PowerShell获取所有SharePoint网站的所有者:
SharePoint Online: Get the Site Owner using PowerShell

相关问题