Pscustomobject 添加动态值

Pscustomobject adding dynamic values

我正在尝试从特定 OU 读取计算机的组成员身份并写入 CSV 文件。组成员的输入标准就像计算机是“admin”的一部分,我需要以下格式的 csv 文件

---------------------------------------------------------
Computer   Group1  Group2  Group3 Group4
ABCD       admin1   admin2 admin3 admin4
EFGH                admin2  admin3
XYZZ       admin1                  admin4
--------------------------------------------------------------
but end up like this.
---------------------------------------------------------
Computer   Group1  Group2  Group3 Group4
ABCD       admin1  admin2  admin3 admin4
EFGH       admin2  admin3
XYZZ       admin1  admin4
--------------------------------------------------------------

代码是这样的

$All_computer = Get-ADComputer -Filter * -Property * -SearchBase $ou -Server $server | Select-object Name,DNSHostName,Description,memberof 
$computerExport = $All_computer | 
ForEach-Object {

  $ComputerName = $_.Name
  $Description = $_.description
  $DNSHostname = $_.DNSHostName
  $memberof = $_.memberof
  
$groups = $memberof.where{$_ -like  "*$unput_group*"} 
$Group_member = [pscustomobject]@{
        Workstation = $ComputerName
        Comp_Description = $Description
        DNS_Hostname = $DNSHostname
    }
$i = 0

foreach($group in $Groups) 
{
$i++
$member = $group.split('=')[1].split(',')[0]
$Group_member | add-member -MemberType NoteProperty -Name "Group$i" -Value $member
}

$Group_member
  } 

 }

$computerExport | export-csv .\Membership_status.csv -NoTypeInformation

我需要做什么才能让组成员填充到正确的列中。

当然可以。我的意思是它正在做你要求它做的事情。 您只是将属性的数量添加到从 where 对象查询中找到的自定义对象。我真的很难理解您要这样做的目的,但我认为您真正想要的是每个对象都具有所有可能的属性,但对于那些与该特定计算机不匹配或更好的对象具有空值使用布尔值。

所以...可能是这样的:

[string]$GroupSearch = "admin"

$All_computer = Get-ADComputer -Filter * -Property DNSHostName, Description, memberof -SearchBase $ou -Server $server | Select-Object Name, DNSHostName, Description, memberof 
$MatchedGroups = $All_Computer.MemberOf | Sort -Unique | ?{$_.Name -match $GroupSearch}

$computerExport = ForEach ($C in $All_computer) {
    $Group_member = [pscustomobject]@{
        Workstation      = $($C.Name)
        Comp_Description = $($C.Description)
        DNS_Hostname     = $($C.DNSHostName)
    }

    ForEach ($group in $MatchedGroups) {
        [string]$GrpName = $($group.split('=')[1].split(',')[0])
        If ($C.MemberOf -contains $group) {
            $Group_member | Add-Member -MemberType NoteProperty -Name $GrpName -Value $true
        } else {
            $Group_member | Add-Member -MemberType NoteProperty -Name $GrpName -Value $false
        }
    }
    $Group_member
} 

$computerExport | Export-Csv .\Membership_status.csv -NoTypeInformation

如果我理解这个问题,您需要从某个 OU 中获取属于具有相似部分名称的组成员的所有计算机。 为此,我建议首先创建一个计算机对象数组,其中包含一个名为 'Groups' 的临时额外 属性,其中存储与部分名称匹配的组名称。

稍后,我们将按正确的顺序将它们作为名为 'Group1'、'Group2' 等的新属性放置

# the partial groupname to search for
$unput_group = 'admin'

# Get-ADComputer by default already returns these properties:
# DistinguishedName, DNSHostName, Enabled, Name, ObjectClass, ObjectGUID, SamAccountName, SID,  UserPrincipalName

# get an array of computer objects that are member of 'admin*' group(s) with the desired properties
# one extra temporary property is added which contains an array of 'admin*' group names
$All_computer = Get-ADComputer -Filter * -Property Description, MemberOf  -SearchBase $ou -Server $server |
                Where-Object { $_.MemberOf -match $unput_group} |
                Select-Object @{Name = 'Workstation'; Expression = {$_.Name}},
                              @{Name = 'Comp_Description'; Expression = {$_.Description}},
                              @{Name = 'DNS_Hostname'; Expression = {$_.DNSHostName}},
                              @{Name = 'Groups'; Expression = { @($_.MemberOf |
                                        Where-Object { $_ -match "CN=($unput_group[^,]+)" } | 
                                        ForEach-Object { $matches[1] }) }}

# get all the group names from the computers we have collected and sort unique
$All_Groups = $All_computer.Groups | Sort-Object -Unique

# build a lookup hashtable with property names ('Group1', 'Group2' etc)
$hash = [ordered]@{}
for ($i = 1; $i -le $All_Groups.Count; $i++) {
    $hash["Group$i"] = $All_Groups[$i - 1]
}

# now loop through the collection and add the group properties in order
$result = foreach ($computer in $All_computer) {
    foreach ($propertyName in $hash.Keys) {
        $group = if ($computer.Groups -contains $hash[$propertyName]) { $hash[$propertyName] }
        $computer | Add-Member -MemberType NoteProperty -Name $propertyName -Value $group
    }
    # output the updated object and remove the temporary 'Groups' property
    $computer | Select-Object * -ExcludeProperty Groups
}

# finally, save the results to disk
$result | Export-Csv -Path .\Membership_status.csv -NoTypeInformation