简体   繁体   English

PowerShell:获取成员所属的所有组,包括组类型

[英]PowerShell: Get all groups a member belongs to, include group type

I have a script that gives me all members of a group with certain desired information. 我有一个脚本,可以为我提供一组所需信息的所有组成员。 I want this same format but for all groups that a specified username belongs to. 我希望使用相同的格式,但是对于指定用户名所属的所有组。 I want information about each group, such as group type (ie security, distribution list). 我需要有关每个组的信息,例如组类型(即安全性,分发列表)。 How would I do this? 我该怎么做? I want a different row for each group, with information about each group in the columns. 我希望每个组都有不同的行,并在列中提供有关每个组的信息。

Add-PSSnapin Quest.ActiveRoles.ADManagement

$myCol = @()
  ForEach ($Group in (Get-QADGroup "CN=research,OU=Security,OU=Groups,DC=xxx,DC=com" -GroupType Security))
{
ForEach ($Member in (Get-QADGroupMember $Group -SizeLimit 0))
    { 
    $myObj = "" | Select Group, Type, Member, Email, Username, Department
    $myObj.Group = $Group.Name
    $myObj.Type = $Group.groupType
    $myObj.Member = $Member.Name
    $myObj.Email = $Member.Email
    $myObj.Department = $Member.Department
    $myObj.Username = $Member.sAMAccountName
    $myCol += $myObj
    }
}
 $myCol | Export-Csv -Path "C:\Users\sdevito\Desktop\test.csv" -NoTypeInformation

or. 要么。 there is this code that i found that does something similar, but each group is in the same row, different column. 我发现有这段代码执行类似的操作,但是每个组在同一行,不同列中。 i cannot figure out how to edit this code to make each group on a new row. 我不知道如何编辑此代码以使每个组都在新行上。

$alist = "Name`tAccountName`tDescription`tEmailAddress`tLastLogonDate`tManager`tTitle`tDepartment`tCompany`twhenCreated`tAcctEnabled`tGroups`n"
$userlist = Get-ADUser sdevito -Properties * | Select-Object -Property Name,SamAccountName,Description,EmailAddress,LastLogonDate,Manager,Title,Department,Company,whenCreated,Enabled,MemberOf | Sort-Object -Property Name
$userlist | ForEach-Object {
$grps = $_.MemberOf | Get-ADGroup | ForEach-Object {$_.Name} | Sort-Object
$arec = $_.Name,$_.SamAccountName,$_.Description,$_.EmailAddress,$_LastLogonDate,$_.Manager,$_.Title,$_.Department,$_.Company,$_.whenCreated,$_.Enabled
$aline = ($arec -join "`t") + "`t" + ($grps -join "`t") + "`n"
$alist += $aline
}
$alist | Out-File C:\Users\sdevito\Desktop\testt.csv

How about something like: 怎么样:

#Requires -Version 3.0

Add-PSSnapin Quest.ActiveRoles.ADManagement

function Get-UsersGroups {
    [cmdletbinding()]
    param (
        [Parameter(Position=0,Mandatory)][string]$Identity,
        [Parameter(Position=1)][ValidateSet('all','nested','normal')][string]$MemberType
    )

    $user = Get-QADUser -Identity $Identity

    switch ( $MemberType ) {
        'all' { $groups = $user.AllMemberOf }
        'nested' { $groups = $user.NestedMemberOf }
        default { $groups = $user.MemberOf }
    }

    foreach ( $group in $groups ) {
        $groupinfo = Get-QADGroup -Identity $group
        $props = [ordered]@{
            Group = $groupinfo.Name
            Type = $groupinfo.GroupType
            Member = $user.Name
            Email = $user.Email
            Department = $user.Department
            Username = $user.sAMAccountName
        }
        $obj = New-Object -TypeName PSObject -Property $props
        $obj
    }
}

Get-UsersGroups -Identity bob | Export-Csv -Path "C:\Users\sdevito\Desktop\test.csv" -NoTypeInformation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据组扩展属性获取用户所属的所有组 - How to get all groups that a user is a member of, based on group extensionattribute Powershell:获取属于特定组的所有成员及其组 - Powershell: Get all members and their groups that are members of a specific group 如何使用Powershell脚本在Outlook 2010中获取属于发件人组的所有电子邮件地址 - How to get all the email addresses that belongs to the sender's group, in outlook 2010, using powershell scripts 如何仅显示组用户的组名是Powershell中的成员 - How to display just the group name of groups user is member of in powershell powershell 脚本导出所有组和成员以及组成员中的组 - powershell script to export all groups and members and the groups with in the group members 如何获取邮件联系人所属的所有组? - How to get all groups that a mail-contact belongs to? 如何通过 powershell 获取具有多个子组的一个 AD 组中的所有成员? - How can I get all of the members within one AD Group with many child groups by powershell? 如何获取用户所属的所有组? - How to get all groups that a user is a member of? 如何获取邮件联系人所属的所有组? - How to get all groups that a mail contact is a member of? Powershell 获取组、成员和成员的详细信息 - Powershell to get group, members and member of details
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM