2016-12-26 1 views
0

In my other question Ich fragte, wie man den Typ des Benutzerkontos bestimmen kann. Aber jetzt muss ich herausfinden, welche Art von Gruppe ich haben könnte.Wie bestimmt man den Gruppentyp?

Ich schreibe eine Funktion, die einen Benutzer zu einer Gruppe hinzufügen wird. Das Problem ist, dass ich wissen muss, welche Art von Gruppe es ist, bevor ich den Benutzer hinzufügen kann. Es kann sich um eine moder o365-Gruppe, eine Sicherheitsgruppe, eine Verteilerliste oder eine E-Mail-aktivierte Sicherheitsgruppe handeln.

Also, ich mache etwas wie folgt aus:

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

Aber das Problem dabei ist, dass ich weiß, muss, welche Art von Gruppe ist es, bevor ich auf die Gruppe handeln kann.

Wie kann ich herausfinden, welche Art von Gruppe ich habe?

Antwort

1

Hier ist das Beste, was ich mir vorstellen konnte. Aber es funktioniert [klopft sich selbst auf den Rücken].

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