powershell 如何确定集团类型?

t40tm48m  于 2023-06-23  发布在  Shell
关注(0)|答案(2)|浏览(106)

In my other question我问如何确定用户帐户的类型。然而,现在我需要发现我可能拥有什么样的团队。
我正在编写一个函数,将用户添加到一个组。问题是我需要知道它是什么类型的组,然后才能将用户添加到其中。它可能是modero365组、安全组、通讯组列表或启用邮件的安全组。
所以我在做这样的事情:

function GetGroupType([string]$groupname){
    #You can use get-group on any group, but the 'grouptype' results are not very descriptive
    #mesg = "Universal, SecurityEnabled"
    #o365 = "Universal"
    #Distribution = "Universal"
    $thisgrouptype = get-group -identity $groupname -ErrorAction SilentlyContinue | select grouptype
    if($thisgrouptype.contains("Universal"){
        if($thisgrouptype.contains("SecurityEnabled"){
            $grouptype = "mesg"
        }else{
            #since you can't run get-unifiedgroup against a security/distribution group, we should get "Universal" for the return value
            $thisgrouptype = get-unifiedgroup -identity $groupname -ErrorAction SilentlyContinue | select grouptype
            if($thisgrouptype -eq "Universal"){
                $grouptype = "o365"
            }else{
            $grouptype = "distribution"
        }
    }else{  #if it doesn't have "Universal" then it's not what we are looking for
        $grouptype = ""
    }

    return $grouptype
}

    #try to add a user to a group
    $grouptype = GetGroupType $groupname
    switch($grouptype){
        "o365"{Add-UnifiedGroupLinks -identity "$whatgroupname" -LinkType Members -Links "$whatusername"  }
        "mesg"{Add-DistributionGroupMember -Identity "$whatgroupname" -Member "$whatusername"  }
    }

但这样做的问题是,我必须知道它是什么样的群体,然后才能对群体采取行动。
我怎样才能知道我有什么样的组?

iq0todco

iq0todco1#

非常讨厌微软的一部分。六年后,我仍然在寻找同样的主题。:D
无论如何,在我的测试中,我发现当你执行Get-group时,M365组和通讯组列表中有一个参数不同。这就是RecipientTypeDetails。
对于通讯组列表,RecipientTypeDetails是MailUniversalDistributionGroup,对于M365组,它是GroupMailbox。希望能帮助到一些人…:D

m1m5dgzv

m1m5dgzv2#

这是我能想到的最好的。但它确实起作用[拍拍自己的背]。

#figure out what kind of group we have and return:  mesg | o365 | distribution
function GetGroupType([string]$groupname){
    #You can use get-group on any group, but the 'grouptype' results are not very descriptive
    #mesg = "Universal, SecurityEnabled"
    #o365 = "Universal"
    #Distribution = "Universal"
    $thisgrouptype = get-group -identity $groupname -ErrorAction SilentlyContinue | select grouptype
    if($thisgrouptype.grouptype.contains("Universal")){
        if($thisgrouptype.grouptype.contains("SecurityEnabled")){
            $grouptype = "mesg"
        }else{
            #since you can't run get-unifiedgroup against a security/distribution group, we should get "Universal" for the return value
            #so, just check to see whether it is a unified group
            #$thisgrouptype = get-unifiedgroup -identity $whatgroupname -ErrorAction SilentlyContinue | select grouptype
            $isUnified = [bool](get-unifiedgroup -identity $groupname -ErrorAction SilentlyContinue)
            if($isUnified){
                $grouptype = "o365"
            }else{
                $grouptype = "distribution"
            }
        }
    }else{  #if it doesn't have "Universal" then it's not what we are looking for
        $grouptype = ""
    }

    return $grouptype
}

相关问题