Powershell 剥离不常见的对象属性
Powershell stripping uncommon object properties
我有一个 Exchange 脚本,它为我提供有关我们数据库的激活首选项的信息。问题是每个数据库可以有 3 到 5 个副本,当我 运行 以下脚本时,"pref5" 属性 被删除。我通常会通过说它正在剥离不常见的对象属性来合理化这一点,但事实并非如此,因为某些对象只有 3 个副本 - 因此只有 3 "pref" 属性,但具有 4 个副本的数据库显示得很好。我的输出 csv 以名称、pref1、pref2、pref3、pref4 结尾(为什么不是 pref5?)。我已经测试了 运行ning 并捕获了数组,并且 pref5 属性 is 在那里,当你将它分组时,它不会进入管道其他没有 pref5 的对象。
$dbs = Get-MailboxDatabase "E2K10_DB*" | sort name
foreach ($db in $dbs) {
$db | select -ExpandProperty ActivationPreference | foreach {
$db | Add-Member -Type 'NoteProperty' -Name ("pref" + $_.value) -Value $_.key.tostring()
}
}
$dbs | select name,pref? | Export-Csv $home\Desktop\DBPrefs.csv -NoTypeInformation
好的。这应该是您正在寻找的东西的开始。我的 CAS 是 运行 2.0 PowerShell,所以我确保解决方案受支持。如果你有 3.0 或更高版本,它可能会更简洁,但它应该仍然可以工作。
$dbs = Get-MailboxDatabase "E2K10_DB*" | Sort Name
$prefCount = $dbs | Select -ExpandProperty ActivationPreference | Measure-Object value -Maximum | Select -ExpandProperty Maximum
$results = $dbs | ForEach-Object{
$props = @{}
$name = $_.Name
$props.Name = $name
$dbActivationPreference = $_ | Select -ExpandProperty ActivationPreference
For($prefIndex = 1;$prefIndex -le $prefCount;$prefIndex++){
$indexValue = $dbActivationPreference | Where-Object{$_.Value -eq $prefIndex} | Select-Object -ExpandProperty Key
$props."pref$prefIndex" = $indexValue
}
New-Object -TypeName PSCustomObject -Property $props
}
$propsOrder = @("Name") + (1..$prefCount | ForEach-Object{"pref$_"})
$results | Select $propsOrder | Export-Csv $home\Desktop\DBPrefs.csv -NoTypeInformation
我认为您遇到的问题是某些数据库没有同一组 ActivationPreference
。所以我们需要计算出最大偏好,这样我们就知道有很多 pref#
我们必须添加。
$prefCount
将从具有最高偏好计数的数据库中获取数字。现在我们知道每个实体需要多少个值了。
$props = @{}
是我们用来构建包含所有首选项和数据库名称的 属性 列表的内容。
我们捕获 $results
并确保输出中属性的顺序我们必须使用 $propsOrder
我有一个 Exchange 脚本,它为我提供有关我们数据库的激活首选项的信息。问题是每个数据库可以有 3 到 5 个副本,当我 运行 以下脚本时,"pref5" 属性 被删除。我通常会通过说它正在剥离不常见的对象属性来合理化这一点,但事实并非如此,因为某些对象只有 3 个副本 - 因此只有 3 "pref" 属性,但具有 4 个副本的数据库显示得很好。我的输出 csv 以名称、pref1、pref2、pref3、pref4 结尾(为什么不是 pref5?)。我已经测试了 运行ning 并捕获了数组,并且 pref5 属性 is 在那里,当你将它分组时,它不会进入管道其他没有 pref5 的对象。
$dbs = Get-MailboxDatabase "E2K10_DB*" | sort name
foreach ($db in $dbs) {
$db | select -ExpandProperty ActivationPreference | foreach {
$db | Add-Member -Type 'NoteProperty' -Name ("pref" + $_.value) -Value $_.key.tostring()
}
}
$dbs | select name,pref? | Export-Csv $home\Desktop\DBPrefs.csv -NoTypeInformation
好的。这应该是您正在寻找的东西的开始。我的 CAS 是 运行 2.0 PowerShell,所以我确保解决方案受支持。如果你有 3.0 或更高版本,它可能会更简洁,但它应该仍然可以工作。
$dbs = Get-MailboxDatabase "E2K10_DB*" | Sort Name
$prefCount = $dbs | Select -ExpandProperty ActivationPreference | Measure-Object value -Maximum | Select -ExpandProperty Maximum
$results = $dbs | ForEach-Object{
$props = @{}
$name = $_.Name
$props.Name = $name
$dbActivationPreference = $_ | Select -ExpandProperty ActivationPreference
For($prefIndex = 1;$prefIndex -le $prefCount;$prefIndex++){
$indexValue = $dbActivationPreference | Where-Object{$_.Value -eq $prefIndex} | Select-Object -ExpandProperty Key
$props."pref$prefIndex" = $indexValue
}
New-Object -TypeName PSCustomObject -Property $props
}
$propsOrder = @("Name") + (1..$prefCount | ForEach-Object{"pref$_"})
$results | Select $propsOrder | Export-Csv $home\Desktop\DBPrefs.csv -NoTypeInformation
我认为您遇到的问题是某些数据库没有同一组 ActivationPreference
。所以我们需要计算出最大偏好,这样我们就知道有很多 pref#
我们必须添加。
$prefCount
将从具有最高偏好计数的数据库中获取数字。现在我们知道每个实体需要多少个值了。
$props = @{}
是我们用来构建包含所有首选项和数据库名称的 属性 列表的内容。
我们捕获 $results
并确保输出中属性的顺序我们必须使用 $propsOrder