我将不得不改变输出,使其成为一个真正的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"
答案 0 :(得分:0)
我不完全确定我是否得到了你需要的东西。但如果我做对了你就可以从这样的事情开始:
Get-ADGroup -Filter "Name -like 'SSL_VPN_*'" |
ForEach-Object {
$Members = Get-ADGroupMember -Identity $_ -Recursive
[PSCustomObject]@{
Group = $_.Name
Members = $Members.sAMAccountName
count = $Members.sAMAccountName.count
}
}
这样您就可以获得组,成员和成员的数量。您可以将它管道输送到您需要的任何其他步骤。 (Sort-Object,Export-CSV ...)