在PowerShell中导出为CSV时的列顺序-控制从哈希表创建的自定义对象的属性枚举顺序 [英] Column ordering when exporting to CSV in PowerShell - controlling the property enumeration order of custom objects created from hashtables
本文介绍了在PowerShell中导出为CSV时的列顺序-控制从哈希表创建的自定义对象的属性枚举顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在Powershell
中编写一个脚本,该脚本将从Active Directory
中导出所有securitygroups
及其members
.现在,我要格式化.csv
的输出.
I'm writing a script in Powershell
that exports all securitygroups
and their members
from Active Directory
. Now I want to format the output of the .csv
.
代码:
$Groups = Get-ADGroup -Properties * -Filter * -SearchBase "OU=SERVICES,DC=XXXXXX,DC=XXXXX"
$Table = @()
$Record = @{
"Group Name" = ""
"Name" = ""
"Username" = ""
}
Foreach($G In $Groups)
{
$Arrayofmembers = Get-ADGroupMember -identity $G -recursive | select name,samaccountname
Foreach ($Member in $Arrayofmembers)
{
$Record."Group Name" = $G.Name
$Record."Name" = $Member.name
$Record."UserName" = $Member.samaccountname
$objRecord = New-Object PSObject -property $Record
$Table += $objrecord
}
}
$Table | export-csv "C:\temp\SecurityGroups.csv" -NoTypeInformation
结果:
"Username","Name","Group Name"
"aman","Ani Manoukian","Commercial"
"adan","Aurelia Danneels","Commercial"
"kdeb","Kathleen De Backer","Commercial"
"TVGR","Thijs Van Grimbergen","Commercial"
"SVDE","Sofie Van den Eynde","Commercial"
现在,我希望输出格式为:
"Group Name","Name","Username"
而不是"Username","Name","Group Name"
有人可以帮我吗?
谢谢.
推荐答案
这应该有效...
$Table |
Select-Object "Group Name", "Name", "Username" |
Export-Csv "C:\temp\SecurityGroups.csv" -NoTypeInformation
这篇关于在PowerShell中导出为CSV时的列顺序-控制从哈希表创建的自定义对象的属性枚举顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文