【问题标题】:Adding data to a PowerShell array / Add-Member将数据添加到 PowerShell 数组/添加成员
【发布时间】:2014-07-23 14:03:03
【问题描述】:

我正在尝试编写一个 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?

【问题讨论】:

    标签: powershell active-directory active-directory-group


    【解决方案1】:

    看起来您需要使用以下行向表中添加行(二维数组)。

    $arrGroupMemberList += ,($strTempGroupName, $_.Name)

    http://blogs.msdn.com/b/powershell/archive/2007/01/23/array-literals-in-powershell.aspx

    【讨论】:

    • 谢谢,但试过了,还是没用。我发现了一些新信息并将更新我的问题
    【解决方案2】:

    我发现我做错了什么 - 我使用了错误的 Add-Member。我试图使用 Add-Member 将成员添加到集合中,但它似乎不起作用。这么简单的事情,但我真的没有看到它在任何地方讨论过。所以我找到了一些例子,做了反复试验,让它发挥作用。所以我想在这里发布一个更新,以防其他人有同样的问题。下面的代码就像我想要的那样工作(并将创建一个数组,其中包含来自 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
                }
                }
        }
        }
    

    【讨论】:

      猜你喜欢
      • 2010-11-25
      • 1970-01-01
      • 2015-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多