我正在尝试编写一个PowerShell脚本,该脚本将在Active Directory中与每个组的成员一起编译一个组列表。我的最终目标是将其导出为CSV文件,因此我希望最终的PowerShell多维数组具有以下格式:
GroupName GroupMember
Domain Admins Henry Doe
Domain Admins Melody Doe
Domain Names Doe Ray Me
Domain Users John Doe
Domain Users Jane Doe
(etc…)
我正在使用以下代码尝试执行此操作:
[array]$arrGroupMemberList = New-Object PSObject
Add-Member -InputObject $arrGroupMemberList -membertype NoteProperty -Name 'GroupName' -Value ""
Add-Member -InputObject $arrGroupMemberList -membertype NoteProperty -Name 'GroupMember' -Value ""
[array]$arrGroupMemberList = @()
[array]$arrGroupNameObjects = Get-ADGroup -Filter * | Where-Object {$_.Name -Like "Domain*"}
If ($arrGroupNameObjects.Count -ge 1)
{
## Cycle thru each group name and get the members
$arrGroupNameObjects | ForEach-Object {
[string]$strTempGroupName = $_.Name
$arrGroupMemberObjects = Get-ADGroupMember $strTempGroupName -Recursive
If ($arrGroupMemberObjects.Count -ge 1)
{
## Cycle thru the group members and compile into the final array
$arrGroupMemberObjects | ForEach-Object {
$arrGroupMemberList += $strTempGroupName, $_.Name
}
}
}
}
我的问题是,我一直以我的数组作为结尾:
Domain Admins
Henry Doe
Domain Admins
Melody Doe
Domain Names
Doe Ray Me
Domain Users
John Doe
Domain Users
Jane Doe
我尝试了几种不同的方法,但我已经搜索过,但在任何地方都找不到答案。我确信这很简单,但我做错了什么?我可以创建一个包含必要数据的多维数组,就像我想要做的那样吗?如果我改为使用以下内容:
## Cycle thru the group members and compile into the final array
$arrGroupMemberObjects | ForEach-Object {
$arrGroupMemberList[$intIndex].GroupName = $strTempGroupName
$arrGroupMemberList[$intIndex].GroupMember = $_.Name
$intIndex++
我最终得到的错误如下:
Property 'GroupMember' cannot be found on this object; make sure it exists and is settable.
Property 'GroupName' cannot be found on this object; make sure it exists and is settable.
由于
**更新**
我可能已经找到了我的问题所在,可能是在我添加数组成员时。在PowerShell脚本的末尾,我添加了以下代码行:
$arrGroupMemberList | Get-Member
没有属性,我的元素不存在,即使我在脚本前面添加了Add-Member cmdlet。我是否正确使用Add-Member cmdlet?
答案 0 :(得分:1)
看起来您需要使用以下行向表中添加行(二维数组)。
$ arrGroupMemberList + =,($ strTempGroupName,$ _。Name)
http://blogs.msdn.com/b/powershell/archive/2007/01/23/array-literals-in-powershell.aspx
答案 1 :(得分:1)
我弄清楚我做错了什么 - 我错误地使用了添加成员。我尝试使用添加成员将成员添加到集合中,并且它似乎不会那样工作。简单的东西,但我真的没有看到它在任何地方讨论过。所以我找到了一些例子并做了试错事并让它发挥作用。所以我想在这里发布更新,以防其他人有同样的问题。以下代码就像我想要的那样工作(并将创建一个数组,其中包含Active Directory中的组列表以及每个组中的组成员):
[array]$arrGroupNameObjects = Get-ADGroup -Filter * | Where-Object {$_.Name -Like "Domain*"}
If ($arrGroupNameObjects.Count -ge 1)
{
## Cycle thru each group name and get the members
$arrGroupNameObjects | ForEach-Object {
[string]$strTempGroupName = $_.Name
$arrGroupMemberObjects = Get-ADGroupMember $strTempGroupName -Recursive
If ($arrGroupMemberObjects.Count -ge 1)
{
## Cycle thru the group members and compile into the final array
$arrGroupMemberObjects | ForEach-Object {
$objGroupMember = New-Object PSObject
Add-Member -InputObject $objGroupMember -membertype NoteProperty -Name 'GroupName' -Value $strTempGroupName
Add-Member -InputObject $objGroupMember -membertype NoteProperty -Name 'GroupMemberName' -Value $_.Name
[array]$arrGroupMemberList += $objGroupMember
}
}
}
}