2016-12-26 65 views
0

In my other question我問如何確定用戶帳戶的類型。但是,現在我需要找出我可能擁有的組合。如何確定組類型?

我在寫一個將用戶添加到組的功能。問題是,我需要知道它是什麼類型的組之前,我可以添加用戶。它可能是一個Moder 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

這是我能想到的最好的。但它確實有效[拍背自拍]。

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