繁体   English   中英

Get-ADGroupMember输出格式

[英]Get-ADGroupMember Output Formatting

我将不得不更改输出以使其成为可以包含值中逗号的真正的csv。 我想输出到一个Csv文件。 我在语法上遇到麻烦。 我正在使用如下脚本。

2-如何获取DisplayName而不是名称作为输出?

脚本的逻辑:第一行是导出到CSV的组。 将此列表存储到数组中。

接下来的行将是组[1],[2],[3]等的成员的迭代,直到达到成员数最多的组的最大长度。 在每次迭代时,将该行导出CSV到CSV。 如果没有成员,请不要忘记留下逗号。

$groups = Get-ADGroup -filter {name -like "SSL_VPN_*"}
$keys = @()      # List of groups that are used as keys
$data = @{}      # Hashtable with key being group name and value being list of samaccountnames
$output = @()    # Array of lines for the csv output
$mostmembers = 0 # The number of members of the largest group

# Iterate through the groups, create a list of members associated
foreach ($group in $groups)
{
    $members = Get-ADGroupMember $group.name -recursive
    $sams = @()

    foreach ($member in $members) { $sams += $member.samaccountname }

    if ($sams.length -gt $mostmembers) {$mostmembers = $sams.length}

    $keys += $group.name
    $data[$group.name] = $sams
}

#$data | ft -auto

# Now output the data with each group getting a column
# BUG: This did not work as expected, so I built a list of keys manually.
#$keys = $data.Keys

# Header row (bug: commas in a group name would cause problems)
$output += $keys -join ","

# Build each row
for ($rownum = 0; $rownum -lt $mostmembers; $rownum++)
{
    $row = @()
    for ($col = 0; $col -lt $keys.count; $col++)
    {
        # I had to spell this out because my head was hurting, but this could be made more succinct.
        $group = $keys[$col]
        $users = $data[$group]

        if ($users.count -gt $rownum) # This means there is a valid value, hopefully
        {
            $row += $users[$rownum]
        }
        else
        { $row += "" }
    }
    $output += $row -join ","
}

$output  | Export-csv -path "c:\temp\test.csv"  -NTI

输出:

"Length"
"414"
"160"
"156"
"150"
"134"
"139"
"108"
"120"
"122"
"97"
"102"
"96"
"108"
"97"
"99"
"87"
"83"
"73"
"91"
"76"
"61"
"74"
"76"
"72"
"64"
"58"
"49"
"45"
"54"
"49"
"60"
"49"
"57"
"49"
"51"
"64"

我不确定我是否能满足您的需求。 但是,如果我答对了,您可以从以下内容开始:

Get-ADGroup -Filter "Name -like 'SSL_VPN_*'" |
    ForEach-Object {
        $Members = Get-ADGroupMember -Identity $_ -Recursive
        [PSCustomObject]@{
            Group = $_.Name
            Members = $Members.sAMAccountName
            count = $Members.sAMAccountName.count
        }
    }

通过这种方式,您可以获得组,成员和成员数。 您可以将其传送到所需的任何进一步步骤。 (排序对象,导出CSV ...)

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM