将所有Azure AD组及其所有者导出到csv文件

时间:2019-11-15 04:12:39

标签: azure powershell azure-active-directory

我需要一种将所有具有相应所有者的Azure广告组导出到csv文件的方法。下面的代码可以工作,但是csv文件的格式太糟糕了。一切都在一栏中,很难阅读。如何将所有组放在一个列中,将相应的所有者放在相应行的单独列中。任何帮助将不胜感激

 $groups=Get-AzureADGroup -All $true
    ForEach ($group in $groups){
        $Owners = Get-AzureADGroupOwner -ObjectId $group.ObjectId -All $true 
        ForEach ($Owner in $Owners){ 
            Write-output $group.DisplayName "," $Owner.ObjectId "," $Owner.ObjectType $Owner.UserType "," $Owner.UserPrincipalName >> C:\scripts\Owner.csv
        }
    } 

更新的脚本

$array = @()
$Properties=@{}
$Properties.add("GroupDisplayName","1")
$Properties.add("OwnerObjectId","2")
$Properties.add("OwnerObjectType","3")
$Properties.add("OwnerUserType","4")
$Properties.add("OwnerUserPrincipalName","5")
$groups = Get-AzureADGroup -All $true
Foreach($group in $groups){

     $Owners = Get-AzureADGroupOwner -ObjectId $id  -All $true
     $Properties.GroupDisplayName=$group.DisplayName

     if($Owners -ne $null){
       # group has owner
        Foreach($Owner in $Owners){

                $Properties.OwnerObjectId=$Owner.ObjectId
                $Properties.OwnerObjectType=$Owner.ObjectType
                $Properties.OwnerUserType=$Owner.UserType
                $Properties.OwnerUserPrincipalName=$Owner.UserPrincipalName
                $obj=New-Object PSObject -Property $Properties
                $array +=$obj 


        }
     }
     else{
                #group has no owner
                $Properties.OwnerObjectId=$null
                $Properties.OwnerObjectType=$null
                $Properties.OwnerUserType=$null
                $Properties.OwnerUserPrincipalName=$null
                $obj=New-Object PSObject -Property $Properties
                $array +=$obj  



     }

}
$array | export-csv -Path C:\test1234.csv -NoTypeInformation -Encoding UTF8

2 个答案:

答案 0 :(得分:0)

根据您的需要,您可以参考以下脚本:

$array = @()
$Properties=@{}
$Properties.add("GroupDisplayName","1")
$Properties.add("OwnerObjectId","2")
$Properties.add("OwnerObjectType","3")
$Properties.add("OwnerUserType","4")
$Properties.add("OwnerUserPrincipalName","5")
$groups = Get-AzureADGroup -All $true
Foreach($group in $groups){
  $Owners = Get-AzureADGroupOwner -ObjectId $group.ObjectId -All $true
  ForEach ($Owner in $Owners){ 
            $Properties.GroupDisplayName=$group.DisplayName
            $Properties.OwnerObjectId=$Owner.ObjectId
            $Properties.OwnerObjectType=$Owner.ObjectType
            $Properties.OwnerUserType=$Owner.UserType
            $Properties.OwnerUserPrincipalName=$Owner.UserPrincipalName
            $obj=New-Object PSObject -Property $Properties
            $array +=$obj 
  }


}


$array | export-csv -Path E:\test123.csv -NoTypeInformation -Encoding UTF8

enter image description here


更新

根据您的需要,我将更新PowerShell脚本

$array = @()
$Properties=@{}
$Properties.add("GroupDisplayName","1")
$Properties.add("OwnerObjectId","2")
$Properties.add("OwnerObjectType","3")
$Properties.add("OwnerUserType","4")
$Properties.add("OwnerUserPrincipalName","5")
$groups = Get-AzureADGroup -All $true
Foreach($group in $groups){

     $Owners = Get-AzureADGroupOwner -ObjectId $group.ObjectId -All $true
     $Properties.GroupDisplayName=$group.DisplayName

     if($Owners -ne $null){
       # group has owner
        Foreach($Owner in $Owners){

                $Properties.OwnerObjectId=$Owner.ObjectId
                $Properties.OwnerObjectType=$Owner.ObjectType
                $Properties.OwnerUserType=$Owner.UserType
                $Properties.OwnerUserPrincipalName=$Owner.UserPrincipalName
                $obj=New-Object PSObject -Property $Properties
                $array +=$obj 


        }
     }
     else{
                #group has no owner
                $Properties.OwnerObjectId=$null
                $Properties.OwnerObjectType=$null
                $Properties.OwnerUserType=$null
                $Properties.OwnerUserPrincipalName=$null
                $obj=New-Object PSObject -Property $Properties
                $array +=$obj  



     }

}
$array | export-csv -Path E:\test123.csv -NoTypeInformation -Encoding UTF8

enter image description here

答案 1 :(得分:0)

为了创建带有标题和数据行的适当的CSV文件,您需要收集一个 Objects 数组并将其发送到Export-Csv cmdlet。

$groups = Get-AzureADGroup -All $true
$result = foreach ($group in $groups) {
    Get-AzureADGroupOwner -ObjectId $group.ObjectId -All $true | ForEach-Object {
        # output an object with the properties and headernames you need
        # the $_ automatic variable contains 1 owner object in each iteration
        [PsCustomObject]@{
            'Group'     = $group.DisplayName
            'OwnerId'   = $_.ObjectId
            'OwnerType' = $_.ObjectType
            'OwnerUPN'  = $_.UserPrincipalName
        }
    }
} 

# output on screen
$result | Format-Table -AutoSize

# output to CSV
$result | Export-Csv -Path 'C:\scripts\AZGroupOwners.csv' -NoTypeInformation

希望有帮助