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

时间:2014-02-27 18:13:58

标签: powershell active-directory

我有一个脚本,可以为我的所有成员提供某些所需的信息。我想要相同的格式,但是对于指定用户名所属的所有组。我想要有关每个组的信息,例如组类型(即安全性,分发列表)。我该怎么做?我想为每个组添加一个不同的行,并在列中提供有关每个组的信息。

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

或。有这个代码,我发现它做了类似的事情,但每个组都在同一行,不同的列。我无法弄清楚如何编辑此代码以使每个组都在新行上。

$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

1 个答案:

答案 0 :(得分:1)

如下:

#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