本文介绍了如何确定组类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在另一个问题中,我问如何确定用户帐户的类型.但是,现在我需要发现自己可能属于哪种小组.

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.

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

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?

推荐答案

这是我能想到的最好的方法.但这确实可以[拍拍自己靠背].

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
}

这篇关于如何确定组类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:36