Sharepoint 站点、组、用户、权限导出 2 CSV

Sharepoint Site, Groups, User, Permissons export 2 CSV

我找到了以下 Powershell 脚本。该脚本仅适用于导出到 CSV 更改数据的顺序。在 CSV 中,首先是用户,然后是脚本中的其他值。我不再知道如何改变它。也许你们中有人有想法。

$AdminCenterURL = "https://contoso-admin.sharepoint.com/"
$CSVPath = "C:\Temp\sharepoint.csv"
 
Connect-SPOService -url $AdminCenterURL 
 
$GroupsData = @()
 
Get-SPOSite -Limit ALL | ForEach-Object {
    Write-Host -f Yellow "Processing Site Collection:"$_.URL
  
    $SiteGroups = Get-SPOSiteGroup -Site $_.URL
 
    Write-host "Total Number of Groups Found:"$SiteGroups.Count
 
    ForEach($Group in $SiteGroups)
    {
        $GroupsData += New-Object PSObject -Property @{

            'Site URL' = $_.URL
            'Group Name' = $Group.Title
            'Permissions' = $Group.Roles -join ","
            'Users' =  $Group.Users -join ","
        }
    }
}

$GroupsData | Export-Csv $CSVPath -NoTypeInformation -encoding utf8
 
Write-host -f Green "Groups Report Generated Successfully!"

导出应按以下顺序进行:

站点 Url、组名、权限,最后是所有用户

属性在您的代码中出现的顺序是随机的,因为您构造对象输出的方式:一个从哈希表获取其属性的新对象。 (在哈希表 @{} 中,属性没有固定顺序)

您可以通过输出 PsCustomObjects 轻松更改它以生成您需要的顺序。
下面,还直接捕获变量 $GroupsData 中的输出,这比在数组上使用 += 加法要好得多。

$GroupsData = Get-SPOSite -Limit ALL | ForEach-Object {
    Write-Host "Processing Site Collection: $($_.URL)" -ForegroundColor Yellow

    $SiteGroups = Get-SPOSiteGroup -Site $_.URL
    Write-host "Total Number of Groups Found: $($SiteGroups.Count)"

    foreach($Group in $SiteGroups) {
        [PsCustomObject]@{
            'Site URL'    = $_.URL
            'Group Name'  = $Group.Title
            'Permissions' = $Group.Roles -join ","
            'Users'       = $Group.Users -join ","
        }
    }
}

$GroupsData | Export-Csv $CSVPath -NoTypeInformation -Encoding utf8

同样可以使用Select-Object:

实现
$GroupsData = Get-SPOSite -Limit ALL | ForEach-Object {
    # store this url in a variable first
    $siteUrl = $_.URL
    Write-Host "Processing Site Collection: $siteUrl" -ForegroundColor Yellow

    $SiteGroups = Get-SPOSiteGroup -Site $siteUrl
    Write-host "Total Number of Groups Found: $($SiteGroups.Count)"

    foreach ($Group in $SiteGroups) {
        $Group | Select-Object @{Name = 'Site URL'; Expression = {$siteUrl}},
                               @{Name = 'Group Name'; Expression = {$_.Title}},
                               @{Name = 'Permissions'; Expression = {$_.Roles -join ","}},
                               @{Name = 'Users'; Expression = {$_.Users -join ","}}
    }
}

$GroupsData | Export-Csv $CSVPath -NoTypeInformation -Encoding utf8