【问题标题】:Get-ADGroupMember Output FormattingGet-ADGroupMember 输出格式
【发布时间】:2018-09-26 13:59:39
【问题描述】:

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

2 - 我怎样才能得到 DisplayName 而不是 name 作为输出?

脚本的逻辑:第一行是导出到 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"

【问题讨论】:

标签: powershell export-to-csv


【解决方案1】:

我不完全确定我是否得到了你需要的东西。但是,如果我说得对,您可以从以下内容开始:

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 ...)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多