从Azure AD PowerShell导出组类型

f5emj3cl  于 2022-11-10  发布在  Shell
关注(0)|答案(1)|浏览(189)

我正在尝试导出所有Azure AD组、其所有者、描述、电子邮件及其组类型。例如Office 365、安全或分发。我已经成功地将所有内容正确地导出到一个.csv文件中,除了组类型。Get-AzureADGroup将只返回“Group”,而我无法从Get-msolgroup-grouptype获得任何结果。
我一直在使用的脚本:

$array = @()
$Properties=@{}
$Properties.add("GroupDisplayName","1")
$Properties.add("OwnerObjectId","2")
$Properties.add("OwnerObjectType","3")
$Properties.add("OwnerUserType","4")
$Properties.add("OwnerUserPrincipalName","5")
$Properties.add("GroupDescription","6")
$Properties.add("Email","7")
$Properties.add("GroupTypes","8")
$groups = Get-AzureADGroup -All $true
$GroupType = Get-MsolGroup -Grouptype
Foreach($group in $groups){

     $Owners = Get-AzureADGroupOwner -ObjectId $group.ObjectId -All $true
     $Properties.GroupDisplayName=$group.DisplayName
     $Properties.GroupDescription=$group.description
     $Properties.Email=$group.mail
     $Properties.GroupTypes=$group.GroupType
     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:\scripts\Owners13.csv -NoTypeInformation -Encoding UTF8
6vl6ewon

6vl6ewon1#

根据我的研究,命令Get-MsolGroup是Azure AD V1模块的命令:MSOnline。但您使用的其他命令是Azure AD V2模块的命令:AzureAD。它们位于不同的模块中。因此,如果要使用命令Get-MsolGroup,首先需要运行命令Connect-MsolService。例如:

Connect-MsolService
Get-MsolGroup -all | Select-Object DisplayName, GroupType

此外,如果您只想使用AzureAD模块来获取群组类型,我们可以使用命令Get-AzureADMSGroup来获取。但如果我们使用该命令,我们需要根据响应的属性做出一些判断。有关更多详细信息,请参考document

Connect-AzureAD
Get-AzureADMSGroup -All $true | Select-Object DisplayName, GroupTypes,MailEnabled, SecurityEnabled

更新

您可以使用以下脚本来实现您的需求。

connect-AzureAD
$array = @()
$Properties=@{}
$Properties.add("GroupDisplayName","1")
$Properties.add("OwnerObjectId","2")
$Properties.add("OwnerObjectType","3")
$Properties.add("OwnerUserType","4")
$Properties.add("OwnerUserPrincipalName","5")
$Properties.add("GroupDescription","6")
$Properties.add("Email","7")
$Properties.add("GroupTypes","8")
$groups = Get-AzureADGroup -All $true

Foreach($group in $groups){

     $Owners = Get-AzureADGroupOwner -ObjectId $group.ObjectId -All $true
     $Properties.GroupDisplayName=$group.DisplayName
     $Properties.GroupDescription=$group.description
     $Properties.Email=$group.mail

     $result=Get-AzureADMSGroup -Id $group.ObjectId | Select-Object GroupTypes,MailEnabled, SecurityEnabled, DisplayName

     If($result.GroupTypes -contains "Unified"){

            $Properties.GroupTypes="O365"

       }
       elseif($result.SecurityEnabled  ){

            $Properties.GroupTypes="Security"  
       }
       else{
           $Properties.GroupTypes="Distrubution"

       }

     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:\test.csv -Encoding UTF8 -NoTypeInformation

相关问题