How to Determine Group Type?

て烟熏妆下的殇ゞ 提交于 2019-12-12 04:32:09

问题


In my other question I asked how to determine the type of user account. However, now I need to discover what kind of group I might have.

I am writing a function that will add a user to a group. The problem is that I need to know what kind of group it is before I can add the user to it. It might be a moder o365 group, a security group, a distribution list, or a mail-enabled security group.

So, I'm doing something like this:

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

But the problem with this is that I must know what kind of group it is before I can act upon the group.

How can I find out what kind of group I have?


回答1:


Here is the best I could come up with. But it does work [pats self on back].

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


来源:https://stackoverflow.com/questions/41336598/how-to-determine-group-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!