需要脚本来检测对象属性 - MultiValue

时间:2016-03-03 22:49:14

标签: powershell active-directory exchange-server

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

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

1 个答案:

答案 0 :(得分:0)

你提到了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 

请注意,计算出的属性的名称(Label)可以是您想要的;我使用了与原始属性相同的名称。

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

$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
相关问题