如何确定群组类型?

时间:2016-12-26 21:48:33

标签: powershell office365

In my other question我问过如何确定用户帐户的类型。但是,现在我需要发现我可能拥有的群体。

我正在编写一个将用户添加到组的功能。问题是我需要知道在将用户添加到它之前它是什么类型的组。它可能是现代的o365组,安全组,通讯组列表或启用邮件的安全组。

所以,我正在做这样的事情:

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"  }
    }   

但问题在于我必须知道在我对小组采取行动之前它是什么类型的小组。

我怎样才能知道我有哪种群体?

1 个答案:

答案 0 :(得分:1)

这是我能想到的最好的。但它确实有效[拍拍自拍]。

#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
}