【问题标题】:Need Script to Detect object Properties - MultiValue需要脚本来检测对象属性 - 多值
【发布时间】:2016-06-17 12:19:55
【问题描述】:

我主要使用 PowerShell 来处理 Active Directory 和 MS Exchange。

存在使用连接来报告多值对象的示例,但是在脚本中包含这些行似乎很麻烦。我想要一个能够报告所有对象并提供标准属性并检测多阀属性并使用连接字符“%”导出它们的脚本。

【问题讨论】:

    标签: powershell active-directory exchange-server


    【解决方案1】:

    您提到了 AD 和 Exchange,但对于您具体查询的内容以及哪些是“标准”属性仍然含糊其辞。我猜您可能是指 AD 用户属性,但下面的产品更笼统。

    您可以通过将这些属性放入数组并使用Select-Object 来选择要从对象中检索的属性(属性)列表。在此示例中,您确实需要提前知道哪些属性将是多值的:

    # Create an object with regular and multi-valued properties. In your
    # case I guess this will come from a Get-*User cmdlet or similar.
    
    $test = New-Object -TypeName PSOBject -Property @{"one"=1;"two"=2;"multi"=@("a","b")}
    
    # Define the list of properties you want to return from this object.
    # In this list we include a calculated column to pull the multi-valued
    # data out and -join it with the '%' character
    
    $properties = @('one','two',@{Label="multi";Expression={($_.multi) -join "%"}})
    
    # Now we pipe our object through Select-Object to pull out those
    # properties we want, including the calculated one:
    $test | Select-Object $Properties
    
    one two multi
    --- --- -----
      1   2 a%b 
    

    注意,计算属性的名称(标签)可以是任何你想要的;我使用了与原始属性相同的名称。


    编辑:以下代码适用于上面的 $test 对象,将其转换为新对象,其中多值字段是单个字符串,其中“%”连接原始数组成员。请注意,这不使用准备好的表达式来转换 multi 属性;相反,它使用Select-Object 中的-ExpandProperty 开关来即时完成工作。

    $ConvertedProperties = @{}
    
    foreach($Property in ($test | Get-Member -MemberType Properties)){ 
      $Value = (Select-Object -InputObject $test -ExpandProperty $Property.Name) -join "%"
      $ConvertedProperties.($Property.Name) = $Value
    }
    
    $ConvertedObject = New-Object -TypeName PSOBject -Property $ConvertedProperties
    
    $ConvertedObject | Export-Csv filename.csv -NoTypeInformation
    

    【讨论】:

    • 这是我使用的脚本。我只是希望我可以获取一个广告对象并自动确定属性类型并输出或将多个带有分隔符的值项连接到 CSV。
    • 我想确定属性类型可能是一次性的练习,因为有一个有限的列表,但我理解在一般情况下找到一个解决方案会很好。会考虑一下。
    • 我已经用一个适用于我的简单测试对象的示例更新了我的答案。我希望它是有意义的,能让你更接近与你的 Active Directory 一起工作的东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 2018-05-01
    相关资源
    最近更新 更多