我对powershell非常陌生,我试图创建一个简单的脚本,将组和底层对象导出到csv文件。如果我静态访问数据,例如索引数组中的值但是如何将所有这些更改为动态的呢?
$Groups = Get-QADGroup
$Result = @()
$Groups | ForEach-Object {
$Group = $_
$Members = Get-QADGroupMember $Group -Indirect
$Obj = '' | Select-Object -Property Name, Members #, members2
$Obj.Name = $Group.Name
#$Obj.Members = $Members[0].Name ----would work, but should be dynamic
#$Obj.members2 = $Members[1].Name ----would work, but should be dynamic
$Result += $Obj
}
$Result | Export-Csv -Path C:\Temp\groups.csv -NoTypeInformation -Encoding Unicode -Delimiter ";"
所以我可以写一些像$Obj.Members = $Members
这样的东西,但这不起作用并给我这个输出:
"Name";"Members"
"RootGroup01";"System.Object[]"
"RootGroup02";"System.Object[]"
"RootGroup03";"System.Object[]"
但我喜欢这样的事情(示例数据):
"Name";"Members"
"RootGroup01";"Subuser01";"Subuser02";"SubGroup01"
"RootGroup02";"SubGroup02"
"RootGroup03";"SubGroup03";"Subuser02";"Subuser03";"Subuser04";
...
所有子组/子用户都在$Members
变量
答案 0 :(得分:2)
你实际上几乎就在那里。您所需要的只是迭代$Members
并选择您需要的属性(如@TheMadTechinian建议的那样)加入数组:
$Obj.Members = ($Members | Select-Object -ExpandProperty "Name") -join ';'