将 PowerShell 中的 Multi-Valued 属性导出到 Excel - 尝试使用哈希表数组实现自动化

Exporting Multi-Valued Attributes in PowerShell to Excel - Attempting to Automate Using an Array of Hash Tables

我正在尝试在 PowerShell 中自动展开 multi-valued 属性,以便将信息导出到 excel sheet。启用 multi-valued 导出到 excel 的已知解决方案是为每个 multi-valued 属性手动创建散列 table,效果很好。示例:

Get-ADForest -Server <servername> |

Select-Object @{Name='ApplicationPartitions';Expression={$.ApplicationPartitions}},
              DomainNamingMaster,
              @{Name="Domains";Expression={$.Domains}},
              ForestMode,
              @{Name="GlobalCatalogs";Expression={$.GlobalCatalogs}},
              Name,
              PartitionsContainer,
              RootDomain,
              SchemaMaster,
              @{Name="Sites";Expression={$.Sites}} |

Export-Csv $home'AD Forest Information.csv'

但是,我想为具有许多 multi-valued 属性的命令自动执行此操作。这是我目前所拥有的(我正在使用 Get-ADForest 进行测试):

$Objects = @()

$Object = @{}

Get-ADForest | Get-Member | Where-Object {$_.membertype -eq 'property'} |

Select-Object Name |

ForEach-Object {$Object = @{Name=$_.Name;Expression="$" + "_" + "." + $_.Name} ; $Objects +=$Object}

Write-Output $Objects

Get-ADForest -Server <servername> | Select-Object $Objects | Export-Csv $home\'AD Forest Information.csv'

问题是导出的 .csv 具有正确的列标题,但属性的值仍然 Microsoft.ActiveDirectory.Management.ADPropertyValueCollection 就像它们在不展开属性时一样。

如果您有任何问题,请告诉我。

任何建议都会很棒。

谢谢!

奥斯汀

试试这个作为你的例子,让我知道这是否是你期望的 csv 输出:

cd $Home\Documents

function parseADObject{
param(
    [cmdletbinding()]
    [parameter(mandatory,valuefrompipeline)]
    $InputObject
)
    $properties=($inputObject|gm -MemberType Property).Name
    $parsedObject=@{}

    foreach($prop in $Properties)
    {
        $thisProp=$inputObject.$prop

        if($thisProp -and $thisProp.GetType() -match 'Collection|\[]')
        {
            $val=($inputObject.$prop|out-string).Trim()
        }
        elseif($thisProp -and $thisProp.GetType() -match 'Int|Boolean|Double')
        {
            $val=$thisProp
        }
        elseif($thisProp)
        {
            $val=($thisProp.ToString()).Trim()
        }
        else
        {
            $val=$null
        }

        $parsedObject.$prop=$val
    }

    return $parsedObject|%{New-Object PSObject -Property $_}
}

Get-ADForest|parseADObject|Export-Csv test.csv -NoTypeInformation;ii test.csv

说明,我将在此示例中使用 ADForest:

首先获取要解析的对象的属性:

$adforest=Get-ADForest

PS C:\> $properties=($adforest|gm -MemberType Property).Name

PS C:\> $properties
ApplicationPartitions
CrossForestReferences
DomainNamingMaster
Domains
ForestMode
GlobalCatalogs
Name
PartitionsContainer
RootDomain
SchemaMaster
Sites
SPNSuffixes
UPNSuffixes

然后循环访问对象的属性,询问对象的类型:

PS C:\> foreach($prop in $properties){$adforest.$prop.gettype()}

IsPublic IsSerial Name                                     BaseType                                                         
-------- -------- ----                                     --------                                                         
True     False    ADPropertyValueCollection                System.Collections.CollectionBase                                
True     False    ADPropertyValueCollection                System.Collections.CollectionBase                                
True     True     String                                   System.Object                                                    
True     False    ADPropertyValueCollection                System.Collections.CollectionBase                                
True     True     ADForestMode                             System.Enum                                                      
True     False    ADPropertyValueCollection                System.Collections.CollectionBase                                
True     True     String                                   System.Object                                                    
True     True     String                                   System.Object                                                    
True     True     String                                   System.Object                                                    
True     True     String                                   System.Object                                                    
True     False    ADPropertyValueCollection                System.Collections.CollectionBase                                
True     False    ADPropertyValueCollection                System.Collections.CollectionBase                                
True     False    ADPropertyValueCollection                System.Collections.CollectionBase

然后你只是问它是什么类型的对象:

  • 如果类型与 'Collection or []' 匹配,那么您会将 属性 的值传递给 Out-String 这对于 AD 用户的 proxyAddresses 等属性很有用。
  • ElseIf 类型与 Boolean or Integer or Double 相匹配,那么您将保留该值而不做任何更改。
  • ElseIf 属性 有任何值,也就是说,不是 $null,我正在使用 ToString() method。这对于 ObjectGUID or ObjectSID
  • 这样的属性很有用

请注意,我每次使用 Out-String 或 ToString() 时都使用 Trim() 来消除开头或结尾的任何空格

每个 属性 循环的值,存储在 $val 变量中,然后被添加到 hashTable $parsedObject,然后在循环完成后,变量被转换为PSObject(如果您不这样做,导出将不会按您预期的方式显示,它将显示值的类型而不是实际数据)。