【问题标题】:Sorting properties with PowerShell splatting使用 PowerShell splatting 对属性进行排序
【发布时间】:2019-11-05 14:03:19
【问题描述】:

我正在使用Active Directory module for Windows PowerShell 从 Active Directory 中导出某些值。如何以与列出的属性相同的顺序显示结果?当我运行此命令时,我会按字母顺序列出所有可用的属性。但我想要和期望的是只获取我在哈希表中列出的属性,其顺序与哈希表相同。

$GetADUserOptions = @{
    properties = @(
    "employeeID",
    "employeeNumber",
    "whencreated",
    "wWWHomePage",
    "c",
    "CO"
    )
}
Get-ADUser @GetADUserOptions

我错过了什么?

【问题讨论】:

  • 如果你将你的命令输入select-object $getaduseroptions.properties,你可以控制输出
  • 为什么顺序很重要?

标签: powershell active-directory parameter-splatting


【解决方案1】:

您无法控制 Active Directory 或模块将属性返回给您的顺序。

但是,如果您对生成的数据所做的操作是导出到 CSV(甚至只是控制台)之类的东西,并且您关心列的顺序,那么只需在导出之前使用您想要的顺序使用 Select-Object .

您可以像 @AdminOfThings 这样建议的那样从 splat 传递数组

Get-ADUser @GetADUserOptions | Select-Object $GetADUserOptions.properties

或者您可以显式执行此操作,这还允许对默认情况下不太易于阅读的属性进行一些后处理,例如 lastLogonTimestamppwdLastSet

# assuming lastLogonTimestamp was added to your list of properties
Get-ADUser @GetADUserOptions | Select-Object sAMAccountName,@{
    Label='LastLogonTS'
    Expression={
        [DateTime]::FromFiletime($_.lastLogonTimestamp)
    }
}

【讨论】:

  • 这很漂亮,真的很漂亮?✨坦克!
猜你喜欢
  • 1970-01-01
  • 2022-01-24
  • 2022-01-24
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 2021-08-08
  • 2017-08-09
  • 1970-01-01
相关资源
最近更新 更多