列表是否在CSV文件中启用或禁用AD组成员
我有以下脚本并且运行良好。我试图在最后添加“启用”列,但不确定如何将其添加为扩展属性。
Import-Module ActiveDirectory
#create an empty array
$temp = @()
#make it multi-dimensional array
$Record = @{
"Group Name" = ""
"Name" = ""
"Username" = ""
"Enabled" = ""
}
$Groups = (Get-AdGroup -filter * -SearchBase "CN=Domain Admins,CN..." | Where {$_.name -like "**"} | select name -ExpandProperty name)
Foreach ($Group in $Groups) {
$Arrayofmembers = Get-ADGroupMember -identity $Group -recursive | select name,samaccountname, enabled
foreach ($Member in $Arrayofmembers) {
$Record."Group Name" = $Group
$Record."UserName" = $Member.samaccountname
$Record."Name" = $Member.name
$Record."Enabled" = $Member.enabled
$objRecord = New-Object PSObject -property $Record
$temp += $objrecord
}
}
$temp | export-csv "C:\temp\SecurityGroups.csv" -NoTypeInformation
I have following script and it works fine. I am trying to add 'Enabled' column in the end but not sure how to add it as an expanded property.
Import-Module ActiveDirectory
#create an empty array
$temp = @()
#make it multi-dimensional array
$Record = @{
"Group Name" = ""
"Name" = ""
"Username" = ""
"Enabled" = ""
}
$Groups = (Get-AdGroup -filter * -SearchBase "CN=Domain Admins,CN..." | Where {$_.name -like "**"} | select name -ExpandProperty name)
Foreach ($Group in $Groups) {
$Arrayofmembers = Get-ADGroupMember -identity $Group -recursive | select name,samaccountname, enabled
foreach ($Member in $Arrayofmembers) {
$Record."Group Name" = $Group
$Record."UserName" = $Member.samaccountname
$Record."Name" = $Member.name
$Record."Enabled" = $Member.enabled
$objRecord = New-Object PSObject -property $Record
$temp += $objrecord
}
}
$temp | export-csv "C:\temp\SecurityGroups.csv" -NoTypeInformation
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,
get-adgroupmember
不用启用属性返回对象,因此我们需要为此使用Get-Aduser
。顺便说一句,将项目添加到具有
+=
的数组中是非常时间和内存,因为每次全部数组都需要在内存中重建。让PowerShell从阵列中收集循环的输出要好得多。
Unfortunately, the
Get-ADGroupMember
does not return objects with the Enabled property, so we need to useGet-ADUser
for that.BTW, it is very time and memory consuming to add items to an array with
+=
, because every time the entire array needs to be rebuilt in memory.It is far better to let PowerShell collect the output from the loop in an array.