簡體   English   中英

使用 PowerShell splatting 對屬性進行排序

[英]Sorting properties with PowerShell splatting

我正在使用Windows PowerShell 的 Active Directory 模塊從 Active Directory 導出某些值。 如何以與列出的屬性相同的順序顯示結果? 當我運行此命令時,我會按字母順序列出所有可用的屬性。 但我想要和期望的是只獲取我在哈希表中列出的屬性,其順序與 hash 表的順序相同。

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

我錯過了什么?

您無法控制 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)
    }
}

使用 [ORDERED] 語法將強制數組返回結果的順序。

 $GetADUserPropertiesEAD = [ORDERED]@{
    'Properties' =  'Name',
                    'GivenName',
                    'Surname',
                    'Description',
                    'Enabled',
                    'LastLogonDate',
                    'ObjectClass',
                    'ObjectGUID',
                    'SamAccountName',
                    'SID',
                    'UserPrincipalName',
                    'DistinguishedName',
                    'extensionattribute5'

    } #end $GetADUserProperties

get-aduser -Filter {enabled -eq $False} @GetADUserPropertiesEAD | select $GetADUserPropertiesEAD.Properties 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM